程序运行后立即单击 PyQt 按钮

Posted

技术标签:

【中文标题】程序运行后立即单击 PyQt 按钮【英文标题】:PyQt Button clicked as soon as program ran 【发布时间】:2016-06-30 04:34:31 【问题描述】:

使用 Qt Designer,我创建了一个简单的对话框。我添加了一个名为self.pushButton 的提交按钮。我一直遇到问题,当我运行程序时,会自动单击该按钮。我正在让程序打印到外壳,“单击按钮”,这会在程序运行后立即出现。

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(880, 479)
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(240, 10, 351, 81))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Rockwell Nova Light"))
        font.setPointSize(25)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(60, 50, 741, 101))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Rockwell Nova Light"))
        font.setPointSize(15)
        self.label_2.setFont(font)
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label_3 = QtGui.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(60, 100, 741, 101))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Rockwell Nova Light"))
        font.setPointSize(15)
        self.label_3.setFont(font)
        self.label_3.setAlignment(QtCore.Qt.AlignCenter)
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.comboBox = QtGui.QComboBox(Dialog)
        self.comboBox.setGeometry(QtCore.QRect(340, 260, 161, 51))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Rockwell Nova Light"))
        font.setPointSize(12)
        self.comboBox.setFont(font)
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(714, 412, 121, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Rockwell Nova Light"))
        font.setPointSize(14)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label_4 = QtGui.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(60, 160, 741, 101))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Rockwell Nova Light"))
        font.setPointSize(15)
        self.label_4.setFont(font)
        self.label_4.setAlignment(QtCore.Qt.AlignCenter)
        self.label_4.setObjectName(_fromUtf8("label_4"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

        self.home()
        self.click()

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "Welcome to PURA!", None))
        self.label_2.setText(_translate("Dialog", "This app is your revision partner, helping you learn throughout the day.", None))
        self.label_3.setText(_translate("Dialog", "To begin, first add a question below", None))
        self.comboBox.setItemText(0, _translate("Dialog", "Multiple Choice", None))
        self.comboBox.setItemText(1, _translate("Dialog", "True or False", None))
        self.comboBox.setItemText(2, _translate("Dialog", "Written Question", None))
        self.pushButton.setText(_translate("Dialog", "Submit", None))
        self.label_4.setText(_translate("Dialog", "Select the type of question you would like to make", None))

    def home(self):

        self.pushButton.clicked.connect(self.click)

    def click(self):
        print("button clicked")

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

【问题讨论】:

看起来您正在修改 QtDesigner 生成的文件。您可能想为此文件编写一个包装器,而不是直接写入它。如果您曾经更改过 UI 并想再次将其导出,那么您的手动更改将会丢失。 【参考方案1】:

您在“setupUi”(第 75 行)函数中调用了您的 click 函数,该函数被调用以在开始时设置您的 ui。所以从那里删除self.click()

【讨论】:

以上是关于程序运行后立即单击 PyQt 按钮的主要内容,如果未能解决你的问题,请参考以下文章

单击应用程序按钮时立即运行音频文件 (Android)

单击文件名时 PyQt5 QFileDialog 关闭

PyQT5 与 matplotlib 图,事件循环已经在运行

使 Pyqt 按钮在鼠标按下而不是鼠标按下时运行功能

在程序运行 PyQt 时添加标签

单击按钮后在 PyQt 窗口中显示 PyQt 图表