pyqt研究-点击按钮把你输入的内容显示出来!
kai0200
|
1#
kai0200 发表于 2006-08-14 17:00
pyqt研究-点击按钮把你输入的内容显示出来!
这个是一个简单的程序,不过我觉得对于我实现一些界面的东西很有帮助应为我们一般的时候都是需要让用户输入然后把结果显示出来!
1,先用qt做界面。 我的是414版本的在网上找了找发现qt3的程序很多qt4 的很不好找。只好自己看pyqt带的例子来慢慢研究了! http://blog.chinaunix.net/u/4799/photo/060814163456.jpg File -> New Form -> Widget 选择下列控键托到面板里面 Label Line Edit Push Button 我还修改了Qobject 的名字为 gongzi QLine Edit 的text 为 ”111111“ 程序目的是当你在Line Edit 里面输入的内容,点击Push Button 让你所输入的内容显示在Lable里面。 保存产生的文件到一个目录然后进入win 的cmd方式进入刚才保存ui文件的目录运行一下程序 “c:\Program Files\PyQt4\pyuic4.bat" untitled.ui > ui_gongzi.py 这样就可以产生程序代码文件ui_gongzi.py 我的ui_gongzi.py 的内容 # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'untitled.ui' # # Created: Mon Aug 14 14:26:51 2006 # by: PyQt4 UI code generator 4.0.1 # # WARNING! All changes made in this file will be lost! import sys from PyQt4 import QtCore, QtGui class Ui_gongzi(object): def setupUi(self, gongzi): gongzi.setObjectName("gongzi") gongzi.resize(QtCore.QSize(QtCore.QRect(0,0,400,300).size()).expandedTo(gongzi.minimumSizeHint())) self.verticalLayout = QtGui.QWidget(gongzi) self.verticalLayout.setGeometry(QtCore.QRect(20,120,160,80)) self.verticalLayout.setObjectName("verticalLayout") self.vboxlayout = QtGui.QVBoxLayout(self.verticalLayout) self.vboxlayout.setMargin(0) self.vboxlayout.setSpacing(6) self.vboxlayout.setObjectName("vboxlayout") self.lineEdit = QtGui.QLineEdit(self.verticalLayout) self.lineEdit.setObjectName("lineEdit") self.vboxlayout.addWidget(self.lineEdit) self.verticalLayout_2 = QtGui.QWidget(gongzi) self.verticalLayout_2.setGeometry(QtCore.QRect(20,20,160,80)) self.verticalLayout_2.setObjectName("verticalLayout_2") self.vboxlayout1 = QtGui.QVBoxLayout(self.verticalLayout_2) self.vboxlayout1.setMargin(0) self.vboxlayout1.setSpacing(6) self.vboxlayout1.setObjectName("vboxlayout1") self.label = QtGui.QLabel(self.verticalLayout_2) self.label.setObjectName("label") self.vboxlayout1.addWidget(self.label) self.verticalLayout_3 = QtGui.QWidget(gongzi) self.verticalLayout_3.setGeometry(QtCore.QRect(260,130,111,71)) self.verticalLayout_3.setObjectName("verticalLayout_3") self.vboxlayout2 = QtGui.QVBoxLayout(self.verticalLayout_3) self.vboxlayout2.setMargin(0) self.vboxlayout2.setSpacing(6) self.vboxlayout2.setObjectName("vboxlayout2") self.pushButton = QtGui.QPushButton(self.verticalLayout_3) self.pushButton.setObjectName("pushButton") self.vboxlayout2.addWidget(self.pushButton) self.retranslateUi(gongzi) QtCore.QMetaObject.connectSlotsByName(gongzi) def retranslateUi(self, gongzi): gongzi.setWindowTitle(QtGui.QApplication.translate("gongzi", "Form", None, QtGui.QApplication.UnicodeUTF8)) self.lineEdit.setText(QtGui.QApplication.translate("gongzi", "1111111111", None, QtGui.QApplication.UnicodeUTF8)) # self.label.setText(QtGui.QApplication.translate("gongzi", "TextLabel", None, QtGui.QApplication.UnicodeUTF8)) self.pushButton.setText(QtGui.QApplication.translate("gongzi", "PushButton", None, QtGui.QApplication.UnicodeUTF8)) 2,编写a.py程序 #!/usr/bin/env python #! -*- coding: gb2312 -*- import sys from PyQt4 import QtCore, QtGui from ui_gongzi import Ui_gongzi class Gongzi(QtGui.QWidget): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_gongzi() self.ui.setupUi(self) # self.ui.pushButton.setObjectName("pushButton1") # 调用类的事件 # self.ui.pushButton.connect(self.ui.pushButton, # QtCore.SIGNAL("clicked()"), # self,QtCore.SLOT("close()")) # 按按钮以后把lineEdit 的内容显示到lable self.ui.pushButton.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"),self.printa) # lineEdit 取输入的字段用下面的操作 # a = QtCore.QString(win.text()) self.value = QtCore.QString(self.ui.lineEdit.text()) # lable 输出内容 # self.ui.label.setText(QtGui.QApplication.translate(self.value) self.ui.label.setText(QtGui.QApplication.translate("gongzi", "%s" % self.value, None, QtGui.QApplication.UnicodeUTF8)) def printa(self): print "%s" % self.value self.value = QtCore.QString(self.ui.lineEdit.text()) self.ui.label.setText(QtGui.QApplication.translate("gongzi", "%s" % self.value, None, QtGui.QApplication.UnicodeUTF8)) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) gongzi = Gongzi() # gongzi.ui.pushButton.connect(gongzi.ui.pushButton, # QtCore.SIGNAL("clicked()"), # gongzi.ui.pushButton,QtCore.SLOT("close")) gongzi.show() sys.exit(app.exec_()) |