【问题标题】:PyQt4 enable button on text entry, connect windowsPyQt4启用文本输入按钮,连接窗口
【发布时间】:2014-08-17 06:16:44
【问题描述】:

我正在尝试用 python 编写一个 gui 程序,但几乎没有 gui 编程经验。我开始学习 tkinter 并掌握了我的程序所需的大部分知识,但我最近发现了 PyQt 和 Qt 设计器,输出看起来好多了。

在我的程序中,我想首先打开一个小窗口,提示用户输入一些信息(它将用于加载文件或创建新文件)。因为此信息至关重要,我不希望用户能够在不输入第一个小窗口的情况下继续前进,因此我希望最初禁用“确定”按钮,并在用户在字段中输入信息时启用。我对此进行了尝试(主要是在 Qt Designer 中创建然后编辑),如下所示。

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'open.ui'
#
# Created: Wed Jun 25 17:51:25 2014
#      by: PyQt4 UI code generator 4.10.3
#
# WARNING! All changes made in this file will be lost!

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_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.setEnabled(True)
        Form.resize(308, 143)
        self.horizontalLayout = QtGui.QHBoxLayout(Form)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.horizontalLayout_3 = QtGui.QHBoxLayout()
        self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3"))
        spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_3.addItem(spacerItem)
        self.label = QtGui.QLabel(Form)
        self.label.setObjectName(_fromUtf8("label"))
        self.horizontalLayout_3.addWidget(self.label)
        spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_3.addItem(spacerItem1)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_7 = QtGui.QHBoxLayout()
        self.horizontalLayout_7.setObjectName(_fromUtf8("horizontalLayout_7"))
        self.label_2 = QtGui.QLabel(Form)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.horizontalLayout_7.addWidget(self.label_2)
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.horizontalLayout_7.addWidget(self.lineEdit)
        self.verticalLayout.addLayout(self.horizontalLayout_7)
        self.horizontalLayout_8 = QtGui.QHBoxLayout()
        self.horizontalLayout_8.setObjectName(_fromUtf8("horizontalLayout_8"))
        self.label_3 = QtGui.QLabel(Form)
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.horizontalLayout_8.addWidget(self.label_3)
        self.lineEdit_2 = QtGui.QLineEdit(Form)
        self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
        self.horizontalLayout_8.addWidget(self.lineEdit_2)
        self.verticalLayout.addLayout(self.horizontalLayout_8)
        self.horizontalLayout_9 = QtGui.QHBoxLayout()
        self.horizontalLayout_9.setObjectName(_fromUtf8("horizontalLayout_9"))
        self.pushButton_2 = QtGui.QPushButton(Form)
        self.pushButton_2.setEnabled(False)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.horizontalLayout_9.addWidget(self.pushButton_2)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.horizontalLayout_9.addWidget(self.pushButton)
        self.verticalLayout.addLayout(self.horizontalLayout_9)
        self.horizontalLayout.addLayout(self.verticalLayout)

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.lineEdit_2, QtCore.SIGNAL(_fromUtf8("textEdited(QString)")), self.pushButton_2.setEnabled)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.label.setText(_translate("Form", "Please enter your name and student number:", None))
        self.label_2.setText(_translate("Form", "Name: ", None))
        self.label_3.setText(_translate("Form", "Student number: ", None))
        self.pushButton_2.setText(_translate("Form", "OK", None))
        self.pushButton.setText(_translate("Form", "Cancel", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

当我运行程序并在学号字段中输入一些内容时,它会给出以下错误:TypeError: QWidget.setEnabled(bool): argument 1 has unexpected type 'str'。我意识到问题在于它将字符串作为输入而不是布尔值,但我不知道如何解决它。

我的问题的第二部分是,当用户在小窗口上单击“确定”时,我希望打开一个更大的新窗口,这个窗口将在底部有一个下一个选项,它会进入另一个类似的窗口,但我不知道如何执行此操作(即链接窗口)。

我该如何做上面提到的事情?或者我应该坚持使用 tkinter,即使在我看来它在美学上是劣等的。谢谢。

【问题讨论】:

    标签: python qt user-interface pyqt


    【解决方案1】:

    首先,您不应该将代码写入从 Qt Designer 生成的 UI 文件中,因为下次您写入它时,pyuic 工具会覆盖该文件。而是将其导入单独的文件。

    此行需要从您的 Ui_File 中删除:

    QtCore.QObject.connect(self.lineEdit_2, QtCore.SIGNAL(_fromUtf8("textEdited(QString)")), self.pushButton_2.setEnabled)
    

    例子:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt4.QtCore import pyqtSlot
    from PyQt4.QtGui import QWidget, QApplication
    #assumes your file is called Ui_Main_Form.py from the pyuic tool
    from Ui_Main_Form import Ui_Form
    
    
    class MainForm(QWidget):
        def __init__(self, parent=None):
            #Initialise
            super(MainForm, self).__init__(parent)
    
            #Setup the UI
            self.ui = Ui_Form()
            self.ui.setupUi(self)
    
            #Now can do any modifications that cant be done in Qt Designer
            #Handle the textChanged signal for QLineEdit
            self.ui.lineEdit_2.textChanged.connect(self.line_edit_text_changed)
    
    
        @pyqtSlot(str)
        def line_edit_text_changed(self, text):
            if text:  # Check to see if text is filled in
                self.ui.pushButton_2.setEnabled(True)
            else:
                self.ui.pushButton_2.setEnabled(False)
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        my_form = MainForm()
        my_form.show()
        sys.exit(app.exec_())
    

    有一些关于 PyQt here 的信号和槽机制的有用信息

    有关如何从此启动窗口,请参阅:How to create new PyQt4 windows from an existing window? 接受的答案。

    希望这能让你开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      相关资源
      最近更新 更多