Python 使用 PyQt 開發 windows 視窗軟體

當我們想要利用 Python 開發 windows 視窗軟體其實有幾種套件可以使用,PyQt、PyGTK、wx、tkinter、pywin等,其中我們採用 PyQt 來小牛刀試一下 windows 視窗軟體開發,相關的Qt 參數及函式都可以在官網上查詢到,本次我們則是安裝 windows 版 python 2.7 (32 bit) 及 PyQt4 python2.7 win 32 版套件,對照安裝好的Python版本是2.7還是3.3 與位元版本 選擇對應的PyQt,這邊我的環境是Python 2.7.9 32bit,所以選擇PyQt4的
PyQt4-4.11.3-gpl-Py2.7-Qt4.8.6-x32,此安裝檔內已經包含SIP與Qt4.8.6,所以事先不需要安裝SIP與Qt,如果要開發給python用的UI檔案,可以使用Qt Creator來設計UI。
或是在PyQt中會提供designer.exe這個程式,是Qt的一部分,一起提供進來,方便做UI的開發



轉換.ui檔為.py檔作為module使用

產生完後的.ui檔,PyQt還提供一個Command指令可以把ui檔案轉換成.py檔,方便我們直接在Python中調用。
使用CMD切換到設計好的ui所在目錄下,執行此指令:
   
pyuic4 your_qt_ui.ui -o output_name.py

即可產生出py檔,pyuic4 是PyQt預設代的指令,此檔案的路徑在C:Python27Libsite-packagesPyQt4中,為一個bat檔案,但是實際調用的是C:Python27Libsite-packagesPyQt4uic下的pyuic.py檔案。
引用ui的py範例

我原先的ui檔案名稱為fmwp_mainwindow.ui,產生完的名稱是fmwp_mainwindow.py,這邊我的主要script檔案為FMWPModel.py
於是在FMWPModel.py中一開始使用fmwp_mainwindow.py的程式如下:
   
import fmwp_mainwindow
from fmwp_mainwindow import Ui_MainWindow
from PyQt4.QtGui import QMainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
if __name__ == __main__:
    app = fmwp_mainwindow.QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

如此即可完成,再執行FMWPModel.py就會顯示GUI視窗了!

以上整理網路相關PyQt工具介紹


接下來則實際提供範例程式來操作如下


# -*- coding:utf-8 -*-
# file: PyQtStandarDialog.py
#
import sys
from PyQt4 import QtCore, QtGui   
class MyWindow(QtGui.QWidget):
    def __init__(self):                            # 起始化方法
        QtGui.QWidget.__init__(self)                    # 呼叫父類別起始化方法
        self.setWindowTitle('PyQt')                    # 設定視窗標題
        self.resize(300,200)                        # 設定視窗大小
        gridlayout = QtGui.QGridLayout()                # 建立佈局元件
        self.label = QtGui.QLabel('StandarDialog example')
        gridlayout.addWidget(self.label, 1, 2)
        self.button1 = QtGui.QPushButton('File')            # 產生Button1
        gridlayout.addWidget(self.button1, 2, 1)
        self.button2 = QtGui.QPushButton('Font')            # 產生Button2
        gridlayout.addWidget(self.button2, 2, 2)
        self.button3 = QtGui.QPushButton('Color')            # 產生Button2
        gridlayout.addWidget(self.button3, 2, 3)
        spacer = QtGui.QSpacerItem(200, 80)
        gridlayout.addItem(spacer, 3, 1, 1, 3)
        self.setLayout(gridlayout)                    # 向視窗中加入佈局元件
        self.connect(self.button1,                     # Button1事件
                QtCore.SIGNAL('clicked()'),
                self.OnButton1)
        self.connect(self.button2,                     # Button2事件
                QtCore.SIGNAL('clicked()'),
                self.OnButton2)
        self.connect(self.button3,                     # Button3事件
                QtCore.SIGNAL('clicked()'),
                self.OnButton3)
    def OnButton1(self):           
        self.button1.setText('clicked')
        filename = QtGui.QFileDialog.getOpenFileName(self, 'Open')    # 建立檔案開啟交談視窗
        if not filename.isEmpty():
            self.label.setText(filename)
    def OnButton2(self):                            # Button2插槽函數
        self.button2.setText('clicked')
        font, ok = QtGui.QFontDialog.getFont()                # 建立字型勾選交談視窗
        if ok:
            self.label.setText(font.key())
    def OnButton3(self):                            # Button3插槽函數
        self.button3.setText('clicked')
        color = QtGui.QColorDialog.getColor()                # 建立彩色選取交談視窗
        if color.isValid():
            self.label.setText(color.name())
app = QtGui.QApplication(sys.argv)
mywindow = MyWindow()
mywindow.show()
app.exec_()



留言

這個網誌中的熱門文章

c語言-關於#define用法

PHP教學 - 資料型態(Data Type) - 上

CMD常用網管指令