首页 文章

无法单击PyQt MenuBar

提问于
浏览
0

我正在尝试学习PyQt5并且当前使用 Exit 函数和 Open 函数制作一个MenuBar . 我目前有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 回答

  • 1

    问题是由于QLabel位于菜单上方,阻止了此次点击事件 . QMainWindow具有特定结构,如下图所示:

    enter image description here

    如果要添加窗口小部件,则必须对中央窗口小部件执行此操作,当您将父窗口传递给窗口小部件时,它将放置在相对于它的位置“(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)
            [...]
    

相关问题