你如何输出通过python的gui输入的信息?

时间:2013-03-28 02:59:09

标签: python pyqt4

这是我第一次尝试在python中使用gui,我需要帮助输出我运行gui时设置的信息。单击提交按钮后,它应该打印出类似于下面的内容。如果还需要任何进一步的信息,我将对其进行编辑。 例如 性别:男性 名称:名称 年龄:# 等

import sys
from PyQt4 import QtGui, QtCore
class Gui(QtGui.QMainWindow):
    def __init__(self):
        super(Gui, self).__init__()
        self.initUI()
    def initUI(self):
        #Frame
        self.setGeometry(100, 100, 400, 600)
        self.setWindowTitle('Python Printer')
        #Button
        self.btn1 = QtGui.QPushButton("Submit", self)
        self.btn1.setGeometry(QtCore.QRect(90, 560, 75, 30))
        self.btn1.clicked.connect(self.buttonClicked)
        self.btn2 = QtGui.QPushButton("Cancel", self)
        self.btn2.setGeometry(QtCore.QRect(170, 560, 75, 30))
        self.btn2.clicked.connect(self.buttonClicked)
        #Text Field
        self.texta = QtGui.QPlainTextEdit("Name",self)
        self.texta.setGeometry(QtCore.QRect(75,155,150,25))
        self.textb = QtGui.QPlainTextEdit("Surname",self)
        self.textb.setGeometry(QtCore.QRect(75,125,150,25))
        self.textc = QtGui.QPlainTextEdit("#",self)
        self.textc.setGeometry(QtCore.QRect(75,215,30,25))
        self.textd = QtGui.QPlainTextEdit("##/##/####",self)
        self.textd.setGeometry(QtCore.QRect(75,275,120,25))
        self.texte = QtGui.QPlainTextEdit("",self)
        self.texte.setGeometry(QtCore.QRect(75,345,150,25))
        #Label
        self.labl1 = QtGui.QLabel("Gender",self)
        self.labl1.setGeometry(QtCore.QRect(75,30,150,25))
        self.labl2 = QtGui.QLabel("Legal Name",self)
        self.labl2.setGeometry(QtCore.QRect(75,100,150,25))
        self.labl3 = QtGui.QLabel("Age",self)
        self.labl3.setGeometry(QtCore.QRect(75,190,150,25))
        self.labl4 = QtGui.QLabel("Birthdate (MM/DD/YYYY)",self)
        self.labl4.setGeometry(QtCore.QRect(75,250,150,25))
        self.labl5 = QtGui.QLabel("Email",self)
        self.labl5.setGeometry(QtCore.QRect(75,320,150,25))
        #Combo Box
        self.combox = QtGui.QComboBox(self)
        self.combox.addItem("Select")
        self.combox.addItem("Male")
        self.combox.addItem("Female")
        self.combox.addItem("Other")
        self.combox.setGeometry(QtCore.QRect(75,50,150,25))
        self.show()
    def buttonClicked(self):
        print("Profile")
        #output
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Gui()
    sys.exit(app.exec_())
if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

def buttonClicked(self):
    print("Profile")

这打印到stdout。如果您想向用户显示一些信息,可以使用QMessageBox::warning

def buttonClicked(self):
    # check the input data
    ...
    if errors:
        QMEssageBox.warning(self, 'Errors', 'Please fix the following errors: ...')
        return
    else:
        # process the input data