PyQt 的交互操作
environment
- Windows
- python 2.7
- PyQt 4.11.4
useful guide / startup
- Introduction to GUI development using Qt, 简单明了的介绍了qt gui的基本特性
- PyQt Tutorial, learn PyQt step by step.
- PyQt: Threading Basics Tutorial, 防止UI阻塞, 使用线程
Generate basic UI file
- use
Qt Designer
to create basic UINew
->Widget
->dragPushButton
toForm
Window- Save it, for example
demo.ui
- make sure
pyuic4.bat
is in the System PATH- input
pyuic4.bat -h
in shell, check the response.
- input
- change
.ui
to.py
file- run
pyuic4.bat -x -o demo.py demo.ui
in the working folder. -o
means output, set the output filename.-x
will generateif __name__ == '__main__':
then can run directlypyuic4.bat demo.ui > demo.py
is also OK.
- run
the sample ofdemo.py
, generated automatically by pyuic4.bat
# -*- coding: utf-8 -*- |
run & check the UI- run
python demo.py
in the working folder. - OR run directly in editor like atom.
- run
click then do sth
- create a new py file, for example
show.py
. input code like:
import sys |
- look at
self.ui.pushButton.clicked.connect(self.handleButton)
it links clicked event with handleButton function. - if you want to resize the window after clicked the button. change
handleButton
to
def handleButton(self): |
click then do heavy work
bad code, UI totally deadfrom PyQt4 import QtGui
import sys
import demo
import time
class MyForm(QtGui.QWidget, demo.Ui_Form):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.startCount)
def startCount(self):
self.pushButton.clicked.disconnect()
self.pushButton.clicked.connect(self.stopCount)
i = 0
while i < 15:
i += 1
self.pushButton.setText(str(i))
print i
time.sleep(1)
def stopCount(self):
self.pushButton.clicked.disconnect()
self.pushButton.clicked.connect(self.startCount)
self.pushButton.setText("PushButton")
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()use thread, can do everything in UI
from PyQt4 import QtGui
from PyQt4.QtCore import QThread, SIGNAL
import sys
import demo
class getCountThread(QThread):
def __init__(self):
QThread.__init__(self)
self.count = 0
def __del__(self):
self.wait()
def run(self):
while self.count < 15:
self.count += 1
self.emit(SIGNAL('showCount(int)'), self.count)
print self.count
self.sleep(1)
class MyForm(QtGui.QWidget, demo.Ui_Form):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.startCount)
def startCount(self):
self.pushButton.clicked.disconnect()
self.get_thread = getCountThread()
self.connect(self.get_thread, SIGNAL("showCount(int)"), self.showCount)
self.connect(self.get_thread, SIGNAL("finished()"), self.done)
self.get_thread.start()
# must be under the self.get_thread.start()
self.pushButton.clicked.connect(self.get_thread.terminate)
def showCount(self, count):
self.pushButton.setText(str(count))
def done(self):
self.pushButton.clicked.disconnect()
self.pushButton.setText("PushButton")
self.pushButton.clicked.connect(self.startCount)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()
- explanation
getCountThread
子线程类, 执行繁重任务MyForm
->startCount
点击按键后执行, 设置好信号, 启动子线程self.connect(self.get_thread, SIGNAL("showCount(int)"), self.showCount)
准备接收来自线程的信号, 并在showCount
更新UIself.connect(self.get_thread, SIGNAL("finished()"), self.done)
准备接收来自线程的结束信号, 然后在done
下更新标记和UIself.get_thread.start()
启动子线程self.pushButton.clicked.connect(self.get_thread.terminate)
将按键的功能设置为终止子线程, 必须放在self.get_thread.start()
下面
原创于 DRA&PHO