2017-11-20 11 views
0

私はPyQt5を学び、現在Exit関数とOpen関数を持つMenuBarを作成しようとしています。私は現在、ショートカットで動作するMenuBarを表示していますが、その上にカーソルを置くとMenuBarをクリックできません。ここに私の現在のコードです:PyQt MenuBarをクリックできません

import sys 
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication, QPushButton, QDesktopWidget, QLabel, QFileDialog 
from PyQt5.QtGui import QIcon 
from PyQt5.QtCore import QCoreApplication 
from win32api import GetSystemMetrics 

CURRENT_VERSION = 0.1 

class Example(QMainWindow): 

    def __init__(self): 
     super().__init__() 

     self.initUI() 

    def initUI(self): 
     self.setWindowTitle('test') 

     window_width = GetSystemMetrics(0) 
     window_height = GetSystemMetrics(1) 

     self.resize(0.6 * window_width, 0.6 * window_height) 
     self.center() 

     self.setWindowIcon(QIcon('Icon.png')) 

     #Exit on menubar 
     exitAct = QAction('&Exit', self) 
     exitAct.setShortcut('Ctrl+Q') 
     exitAct.setStatusTip('Exit applicatiion') 
     exitAct.triggered.connect(qApp.quit) 

     #Open on menubar 
     openAct = QAction('&Open', self) 
     openAct.setShortcut('Ctrl+O') 
     openAct.setStatusTip('Open Directory') 
     openAct.triggered.connect(self.openFile) 

     menubar = self.menuBar() 
     fileMenu = menubar.addMenu('&File') 
     fileMenu.addAction(exitAct) 
     fileMenu.addAction(openAct) 

     btn = QPushButton("Test", self) 
     btn.resize(btn.sizeHint()) 
     btn.move(50, 50) 

     btn.clicked.connect(self.buttonpress) 

     self.label = QLabel(self) 
     self.image = QLabel(self) 
     self.openDirectoryDialog = "" 

     self.show() 

    def center(self): 
     qr = self.frameGeometry() 
     cp = QDesktopWidget().availableGeometry().center() 
     qr.moveCenter(cp) 
     self.move(qr.topLeft()) 

    def openFile(self): 
     self.openDirectoryDialog=ddir = QFileDialog.getExistingDirectory(self, "Get Dir Path") 

    def buttonpress(self): 
     label = QLabel(self) 
     self.label.move(100,150) 
     self.label.setFixedWidth(500) 
     self.label.setFixedHeight(100) 
     self.label.setText(self.openDirectoryDialog) 


if __name__ == '__main__': 

    app = QApplication(sys.argv) 

    w = Example() 

    sys.exit(app.exec_()) 

私は、ファイルを開く機能を追加しただけexitメニュー項目を持っていた前に、それは働いていたが、私は再び働いてさえいることを得ることができません。

答えて

1

この問題は、QLabelsがメニューの上にあり、これのクリックイベントをブロックしているために発生します。

enter image description here

あなたがウィジェットを追加したい場合は、ウィジェットに親を渡すとき、あなたは、中央ウィジェットにそれを行う必要があり、それが配置されますQMainWindowは以下の画像のように特定の構造を持っていますそれに対して `(0、0)の位置にある。

class Example(QMainWindow): 
    [...] 
    def initUI(self): 
     [...] 

     #Exit on menubar 
     exitAct = QAction('&Exit', self) 
     exitAct.setShortcut('Ctrl+Q') 
     exitAct.setStatusTip('Exit applicatiion') 
     exitAct.triggered.connect(qApp.quit) 

     #Open on menubar 
     openAct = QAction('&Open', self) 
     openAct.setShortcut('Ctrl+O') 
     openAct.setStatusTip('Open Directory') 
     openAct.triggered.connect(self.openFile) 

     menubar = self.menuBar() 

     fileMenu = menubar.addMenu('&File') 
     fileMenu.addAction(exitAct) 
     fileMenu.addAction(openAct) 

     centralwidget = QWidget(self) 
     self.setCentralWidget(centralwidget) 
     btn = QPushButton("Test", centralwidget) 
     btn.resize(btn.sizeHint()) 
     btn.move(50, 50) 

     btn.clicked.connect(self.buttonpress) 

     self.label = QLabel(centralwidget) 
     self.image = QLabel(centralwidget) 
     [...] 

ますが、以下の変更を加えます

関連する問題