# -*- coding: utf-8 -*-
from pyqt5.qtcore import qthread, pyqtsignal
import sys
class workthread(qthread):
sinout = pyqtsignal(str)
def __init__(self, obj):
super(workthread, self).__init__()
self.obj = obj
def run(self):
if self.obj == "1":
self.run_obj1()
elif self.obj == "2":
self.run_obj2()
else:
pass
def run_obj1(self):
for i in range(5):
self.sinout.emit("這是obj1第%s次" % i)
self.sleep(1)
def run_obj2(self):
for i in range(5):
self.sinout.emit("這是obj2第%s次" % i)
self.sleep(2)
class guiwindow(qwidget):
def __init__(self, parent=none):
super(guiwindow, self).__init__(parent=parent)
self.resize(500, 500)
self.btn1 = qpushbutton(self)
self.btn1.settext("1開始工作")
self.btn1.pressed.connect(self.start_work1)
self.btn1.move(100, 100)
self.btn2 = qpushbutton(self)
self.btn2.settext("2開始工作")
self.btn2.pressed.connect(self.start_work2)
self.btn2.move(200, 200)
self.thread1 = none
self.thread2 = none
def start_work1(self):
self.thread1 = workthread(obj="1") # 必須作為類例項的乙個屬性,如果去掉self的話,會報錯
self.thread1.sinout.connect(self.show_log1)
print("thread1 id:", id(self.thread1)) # 每執行一次,建立乙個子執行緒物件,互不影響
self.thread1.start()
def start_work2(self):
self.thread2 = workthread(obj="2") # 必須作為類例項的乙個屬性,如果去掉self的話,會報錯
self.thread2.sinout.connect(self.show_log2)
print("thread2 id:", id(self.thread2)) # 每執行一次,建立乙個子執行緒物件,互不影響
self.thread2.start()
def show_log1(self, content):
print("content1:", content)
def show_log2(self, content):
print("content2:", content)
if __name__ == '__main__':
w = guiwindow()
w.show()
Python pyqt5 登入介面
使用python第三方模組pyqy5可以非常簡單方便的設計出簡潔實用的介面,還可以使用 qss 對介面進一步美化。效果展示 完整實現 coding utf 8 from pyqt5 import qtcore,qtgui,qtwidgets class ui form object def setu...
python PyQt5 基本結構
0.匯入需要的包和模組 from pyqt5.qt import 主要包含了我們常用的一些類,彙總到了一塊 import sys 1.建立乙個應用程式物件 2.控制項的操作 建立控制項,設定控制項 大小,位置,樣式.事件,訊號的處理 2.1 建立控制項 當我們建立乙個控制項之後,如果說,這個控制項沒...
python pyqt5安裝失敗問題
以前裝命令都是pip一條命令搞定,會自動安裝依賴的庫,但在安裝pyqt5時卻遇到了問題 could not find a version that satisfies the requirement sip 4.19 from pyqt5 from versions no matching dist...