简体   繁体   English

PyQt 对话框 - 如何在按下按钮后退出?

[英]PyQt dialog - How to make it quit after pressing a button?

Well, I'm writing a small PyQt4 app, it's just a single Yes/No dialog which has to execute an external command (eg 'eject /dev/sr0') and quit.好吧,我正在编写一个小的 PyQt4 应用程序,它只是一个是/否对话框,它必须执行一个外部命令(例如“eject /dev/sr0”)并退出。

The app runs, it executes the command after pressing the "Yes" button, but I cannot make the dialog itself exit when executing the command.应用程序运行,它在按下“是”按钮后执行命令,但在执行命令时我无法让对话框本身退出。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import os
import subprocess
from PyQt4 import QtGui
from PyQt4 import QtCore
from subprocess import call
cmd = 'eject /dev/sr0'

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):

        btn = QtGui.QPushButton('Yes', self)     
        btn.clicked.connect(lambda: os.system(cmd))
        btn.resize(180, 40)
        btn.move(20, 35)       

        qbtn = QtGui.QPushButton('No', self)
        qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        qbtn.resize(180, 40)
        qbtn.move(20, 80) 

        self.setWindowTitle('Test')    
        self.show()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Here is my code.这是我的代码。 When I click "Yes", it calls the 'eject /dev/sr0' command properly, but after that the dialog is still visible.当我单击“是”时,它会正确调用“eject /dev/sr0”命令,但此后对话框仍然可见。 I have to click "No" to close the app I would like it to close automatically when the command is executed.我必须单击“否”关闭应用程序,我希望它在执行命令时自动关闭。 What should I add/modify?我应该添加/修改什么?

btn.clicked.connect(self.close)

那将是我的建议

Replace lambda: os.system(cmd) with a function/method that has multiple statements.lambda: os.system(cmd)替换为具有多个语句的函数/方法。

def buttonClicked(self):
    os.system(cmd)
    QtCore.QCoreApplication.instance().quit()

...
    btn = QtGui.QPushButton('Yes', self)     
    btn.clicked.connect(self.buttonClicked)
...

Step1: in the Main Class needs to be build a "connection": Step1:在Main Class中需要建立一个“连接”:

self.ui.closeButton.clicked.connect(self.closeIt)

Step2: Creating a function like to close: Step2:创建一个类似关闭的函数:

def closeIt(self): 
        self.close()

I named to "closeIt" on purpose because if you name it "close" a conflict will occur.我故意将其命名为“closeIt”,因为如果将其命名为“close”,则会发生冲突。

This solution has the advantage if the created GUI is a plugin for another program (like in my case QGIS), only the active GUI will be closed and not the whole program.如果创建的 GUI 是另一个程序的插件(例如我的 QGIS),则此解决方案具有优势,只有活动的 GUI 将关闭,而不是整个程序。

Subclass QDialog() and then close it using your object.子类 QDialog() 然后使用您的对象关闭它。

class Dialog(QDialog):
    """
        Subclassing QDialog class.
    """
    def __init__(self):
        QDialog.__init__(self)

    def close_clicked(self):
        self.close()

In your main function write following code在您的主函数中编写以下代码

dialogbox = Dialog()  # we subclasses QDialog into Dialog
b1= QPushButton("Close",dialogbox)
b1.clicked.connect(dialogbox.close_clicked)
dialogbox.exec_()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM