在 PyQt 主窗口中处理关闭按钮的正确方法,(红色“X") [英] Proper way to handle the close button in a main window PyQt, (Red "X")

查看:120
本文介绍了在 PyQt 主窗口中处理关闭按钮的正确方法,(红色“X")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是 PyQt 的新手.

First off, I am a complete newbie to PyQt.

我一直在尝试将一个函数链接到主窗口的关闭按钮(窗口角落的红色 x),但没有成功.现在,我的代码看起来像这样:

I have been trying to link a function to the Main Window's close button (the red x in the corner of the window) but I haven't been having any success. Right now, my code looks something like this:

class Ui_MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setupUi(self)
    def setupUi(self, MainWindow):
        #setup code goes here
    def retranslateUi(self, MainWindow):
        #re translation of the GUI code 
    def closeEvent(self, event):
        print "User has clicked the red x on the main window"

在一个单独的主"文件中,我有以下内容:

In a seperate "main" file I have the folowing:

class GUIForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self,parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        #self.ui.ECUStatus_txt = MyWidget.__init__.text_list
        self.threadData()


    if __name__ == "__main__":

        app = QtGui.QApplication(sys.argv)
        myapp = GUIForm()
        myapp.show()
        ret = app.exec_()
        sys.exit(ret)

但是,当我通过命令提示符运行时,当我点击红色 x 时,我看不到打印语句.我知道我是 Qt 的新手,但我看到很多人问这个问题,而且似乎没有一个答案超出上面已经写的内容.

However, when I run via command prompt, I am not able to see the print statement when I hit the red x. I know I am new to Qt, but I have seen quite a few people ask this question, and none of the answers seemed to get beyond what is already written above.

以下答案之一:答案#1答案#2

这两个解决方案都和我的类似,但还是不行

Both of these solutions are similar to mine, but it still doesn't work

尽管答案可能适用于该用户的特定代码,但我和我的 PyQt 同事仍然对我们的推理不起作用感到困惑.PyQt 中内置的Red X 框"是否有定义的按钮名称?我可以像连接其他按钮一样将其连接到其他功能吗?

Despite the answer, which may have worked for that user's specific code, my fellow PyQt colleagues and I are still quite foggy on the reasoning ours is not working. Is there a defined button name for the "Red X box" built into PyQt? Can I connect it to another function the someway I would for other buttons?

推荐答案

您不应该修改从 ui 文件生成的类.相反,您应该创建子类并修改子类.

You shouldn't ever modify the class that was generated from your ui file. Instead you should subclass and modify the subclass.

从代码的外观来看,您实际上是在创建两个 QMainWindow 并且由于错误的一个而捕获了 closeEvent(大概那个是隐藏的?).也就是说,self.ui 是一个没有显示的 QMainWindow,也没有添加到 GUIForm 的 UI 中.相反,您自己明确地使用 Ui_MainWindow.setupUi() 方法,将小部件添加到您自己的 QMainWindow、'GUIForm`.

From the looks of your code, you are actually creating two QMainWindows and the closeEvent is being caught for the wrong one (presumably that one is hidden?). That is self.ui is a QMainWindow that is not being shown, and is not added to the UI of GUIForm. Instead you are using the Ui_MainWindow.setupUi() method explicitly yourself, to add the widgets to your own QMainWindow, 'GUIForm`.

相反,您应该做的是将 Ui_MainWindow 类保留为从 ui 文件生成时的状态,然后将主 python 文件修改为:

Instead, what you should to do is leave your Ui_MainWindow class as it was when it was generated from the ui file, and then modify your main python file to be:

class GUIForm(Ui_MainWindow):
    def __init__(self, parent=None):
        Ui_MainWindow.__init__(self, parent)
        self.threadData()

    def closeEvent(self, event):
        print "User has clicked the red x on the main window"
        event.accept()


if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    myapp = GUIForm()
    myapp.show()
    ret = app.exec_()
    sys.exit(ret)

通过这种方式,您可以扩展自动生成的 UI 文件的行为.这使得从 .ui 文件重新生成 python 文件变得容易,而无需重新添加代码(这正是为什么你永远不应该修改自动生成的 Python 文件的原因)

This way you are extending the behaviour of the auto-generated UI file. This makes it easy to regenerate the python file from the .ui file without having to re-add code (this is precisely why you should never modify the auto-generated Python file)

这篇关于在 PyQt 主窗口中处理关闭按钮的正确方法,(红色“X")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆