The memo blog.

About programming language and useful library.

Python: GUI Programming-I

| Comments

老是有很多想法,無奈時間不是很充足,後來想想,時間就像OO,所以我決定堅持到底,看能不能每天都寫點東西,除了持續一下這個Blog也持續寫Code練習練習,避免練習不足。

PyQtGuiMain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
###
#Created on 2013/2/2

#@author: CYChiang
###
from PyQt4 import QtGui

class PyGui(QtGui.QWidget):
    def __init__(self, parent=None):
        super(PyGui, self).__init__(parent)
        # set component
        Label = QtGui.QLabel("The Label")
        button = QtGui.QPushButton("Click Me")
        lineEdit = QtGui.QLineEdit()
        # configure the layout
        mainLayout = QtGui.QGridLayout()
        mainLayout.addWidget(Label, 0, 0)
        mainLayout.addWidget(lineEdit, 0, 1)
        mainLayout.addWidget(button, 1, 0)
        # add event listener and handle
        button.clicked.connect(self.close)
        # set layout
        self.setLayout(mainLayout)

import sys
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    MainGui = PyGui()
    # setGeometry(x_pos, y_pos, width, height)
    MainGui.setGeometry(400, 200, 800, 600)
    # resize(width, height)
    # MainGui.resize(400, 300)
    # Set the title of program
    MainGui.setWindowTitle('The PyQt GUI')
    # Set Icon
    # MainGui.setWindowIcon(QtGui.QIcon('lucia_2d.png'))
    MainGui.setToolTip('I am message.')
    # Set style
    MainGui.setStyleSheet('background: white')
    # Display 
    MainGui.show()
    # run and exit
    app.exit(app.exec_())

程式執行起來大概長成這樣。整個來說Python在用Qt寫GUI上就跟C/C++寫Qt差不多,很多方法都一樣的使用方式,那在這邊主要先練習一下QWidget有哪些功能以及要怎樣使用Qt的connect,這部分就和之前C/C++用法不太一樣,不知道是不是因為新版本的關係。

附上一點解說,同樣主要針對QWidget。後來發現除非對某些視窗需要特別的方法,不然似乎使用framework提供的方法就很夠用了。之後會慢慢的寫些東西補齊全,至於最後出現哪些東西,我也不知道。

1
2
3
4
5
MainGui.setGeometry(x, y, w, h)                   # 設定程式出現在螢幕的x, y地方以及程式的大小
MainGui.setWindowTitle("String"")                 # 這部分是指定我的視窗名稱要什麼
MainGui.setToolTip("String")                      # 這部分是當滑鼠放在程式當中空白部分,會出現哪些字
MainGui.setWindowIcon(QtGui.QIcon("path"”))       # 裡面可以指定Icon的小圖案
MainGui.setStyleSheet('background: white')        # 指定背景顏色,似乎可以套用個圖片之類的

至於其他的object有提供哪些方法?我想大部份和QWidget提供的差不多,我想。但是在排版上,可以透過一些layout來輔助,如果指定一些特別的位置在程式當中。希望每天可以持續下去!這樣的寫程式方式,當然另外一方面Python用的不是很熟,想要寫些額外的東西有點卡卡,就先熟悉然後繼續練習和強化自己。

Comments