- 论坛徽章:
- 0
|
pyqt4开发步骤
用pyqt4来开发图形界面程序的步骤如下
1:用Qt Designer做好图形界面并保存为x.ui文件
2:用pyqt自带的命令pyuic4 edytor.ui > edytor.py。 这样就得到了一个Ui_notepad类(假定窗口名为notepad)
代码大致如下
from PyQt4 import QtCore, QtGui
class Ui_notepad(object):
def setupUi(self, notepad):
notepad.setObjectName("notepad"
notepad.resize(548, 483)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("sakila.png" ,
QtGui.QIcon.Normal, QtGui.QIcon.Off)
notepad.setWindowIcon(icon)
self.button_open = QtGui.QPushButton(notepad)
self.button_open.setGeometry(QtCore.QRect(20, 20, 251, 61))
self.button_open.setObjectName("button_open"
self.pushButton_2 = QtGui.QPushButton(notepad)
self.pushButton_2.setGeometry(QtCore.QRect(280, 20, 251, 61))
self.pushButton_2.setObjectName("pushButton_2"
self.editor_window = QtGui.QTextEdit(notepad)
self.editor_window.setGeometry(QtCore.QRect(20, 90, 511, 371))
self.editor_window.setObjectName("editor_window"
self.retranslateUi(notepad)
QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL("clicked()" ,
notepad.close)
QtCore.QMetaObject.connectSlotsByName(notepad)
def retranslateUi(self, notepad):
notepad.setWindowTitle(QtGui.QApplication.translate("notepad", "记事本", None, QtGui.QApplication.UnicodeUTF )
self.button_open.setText(QtGui.QApplication.translate("notepad", "打开", None, QtGui.QApplication.UnicodeUTF )
self.pushButton_2.setText(QtGui.QApplication.translate("notepad", "关闭", None, QtGui.QApplication.UnicodeUTF )
3:创建一个start.py文件 做主文件
import sys
from PyQt4 import QtCore, QtGui
from edytor import Ui_notepad #加载生成的图形界面类
class StartQt4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_notepad()#实例化这个类
self.ui.setupUi(self)#调用setupUi方法
QtCore.QObject.connect(self.ui.button_open,QtCore.SIGNAL("clicked()" , self.file_dialog)
#绑定信号/槽 也就是将电机button_open这个按钮的事件,绑定到file_dialog这个函数上
def file_dialog(self):
self.ui.editor_window.setText('aaaaaaaaaa')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQt4()
myapp.show()
sys.exit(app.exec_())
4:发布,新建一个setup.py文件
setup.py如下:
# setup.py
from distutils.core import setup
import py2exe
setup(windows=["loadMain.py"])
5:发布
python setup.py py2exe -p PyQt4,sip
注意:在setup.py中除了导入必需的模块以外,只有一条语句。
distutils.core.setup(windows=['MessageBox.py'])
方括号中就是要编译的脚本名,前边的windows表示将其编译成GUI程序。如果要编译命令行界面的可执行文件,只要将windows改为console
py2exe会在当前目录下生成两个目录 build和dist
build里是一些py2exe运行时产生的中间文件,dist里有最终的可执行文件
library.zip
w9xpopen.exe
python23.dll
hello.exe
不过记得如果要发布到别的机器上时,library.zip、 w9xpopen.exe、python23.dll这几个文件是必须要和hello.exe在一起的。 |
|