美文网首页
PyQt5按钮绑定退出程序的点击事件

PyQt5按钮绑定退出程序的点击事件

作者: _Mirage | 来源:发表于2020-04-01 01:18 被阅读0次

    初步处理事件需要从PyQt5的QtCore库中导入QCoreApplication
    这个QCoreApplication有很多与事件有关的函数,我们这次用到了它的Quit函数作为回调函数,作用就是离开程序。

    与QPushButton的点击事件绑定:
    The slot can be a Qt slot or any Python callable.
    button.clicked.connect(放个回调函数或者Qt slot)

    QCoreApplication, which is retrieved with QApplication.instance(), contains the main event loop—it processes and dispatches all events. The clicked signal is connected to the quit() method which terminates the application. The communication is done between two objects: the sender and the receiver. The sender is the push button, the receiver is the application object.

    代码:

    # coding='utf-8'
    
    from PyQt5.QtWidgets import QWidget, QApplication, QPushButton
    import sys
    
    
    class Gui(QWidget):
        def __init__(self):
            super().__init__()
            self.start()
    
        def start(self):
            button = QPushButton('点我', self)
            button.clicked.connect(QApplication.instance().quit)
            button.resize(button.sizeHint())
            button.move(50, 50)
    
            self.setGeometry(400, 400, 500, 600)
            self.setWindowTitle('初步尝试事件处理')
            self.show()
    
    
    win = QApplication(sys.argv)
    gui = Gui()
    sys.exit(win.exec_())
    
    
    运行结果: image.png

    相关文章

      网友评论

          本文标题:PyQt5按钮绑定退出程序的点击事件

          本文链接:https://www.haomeiwen.com/subject/cginuhtx.html