close
Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": /usr/lib/python2.7/dist-packages/libsvn/_delta.so: failed to map segment from shared object: Cannot allocate memory). Look in the Trac log for more information.
note
- QT creater:QT 的 IDE。整合了原本 QT designed 所有的設計功能。
- PyQT:將 python 與 qt bind 在一起,支援 python 2、3。
- 4.5 版後適用 LGPL licence。
- PySide?: LGBL licence。PyQt? 不是。
- API Reference:
- C:/QtSDK/readme/index.html
- widget:如!QLineEdit、QPushButton。
- layout:用來分配 widget 的排列方式。
PySide
- 手冊:http://developer.qt.nokia.com/wiki/PySideDocumentation/
- 環境:
- QT SDK
- Python
- PySide 1.07
- 測試:
import PySide
print PySide.__version__
結果:1.0.7
- 第一個 Hello World GUI 程式:
# Import PySide classes
import sys
from PySide.QtCore import *
from PySide.QtGui import *
# Create a Qt application
app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
sys.exit()
你也可以在 QLabel 中加入 html 語法
label = QLabel("<font color=red size=40>Hello World</font>")
- 第二個程式,加入connect與程式連結:
#!/usr/bin/python
# 載入 PySide classes
import sys
from PySide.QtCore import *
from PySide.QtGui import *
# 定義一個 say_hello 的 subroutine,這個 subroutine 會印出 Hellow World
def say_hello():
print 'Hellow World'
# 建立 Qt 應用程式
app = QApplication(sys.argv)
# 新增一個 button
button = QPushButton('test')
# 將 button 與 say_hello 連結
button.clicked.connect(say_hello)
# 讓 button 顯示出來
button.show()
# 執行 Qt 應用程式的 main loop
app.exec_()
sys.exit()
- 第三個程式:產生一個 dialog 應用程式
#!/usr/bin/python
# Import PySide classes
import sys
from PySide.QtCore import *
from PySide.QtGui import *
# print hello
def say_hello():
print 'hi.. wade'
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.setWindowTitle('My form')
if __name__ == '__main__':
app = QApplication(sys.argv)
form = Form()
form.show()
sys.exit(app.exec_())
- 使用 layout 來排列 widgets:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Import PySide classes
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
# 建立 widgets
self.edit = QLineEdit('input your name')
self.button = QPushButton('submit')
# 建立 layout 並加入上面的 widgets
layout = QVBoxLayout()
layout.addWidget(self.edit)
layout.addWidget(self.button)
# 設定 dialog layout
self.setLayout(layout)
# 設定 dialog 的說明
self.setWindowTitle('My form')
# 將 button 事件與 say_hellow slot 連結
self.button.clicked.connect(self.say_hellow)
def say_hellow(self):
print ("Hello %s" % self.edit.text())
if __name__ == '__main__':
# 建立 QT 應用程式
app = QApplication(sys.argv)
# 建立並顯示 form
form = Form()
form.show()
# 執行主要的 QT loop
sys.exit(app.exec_())
Reference
Download in other formats: