pyside6界面操作模板
- 编程
- 2021-12-23
- 1608
from PySide6.QtWidgets import QApplication, QMessageBox
from PySide6.QtUiTools import QUiLoader
from threading import Thread
import requests, re
from PySide6.QtCore import Signal, QObject
# 自定义信号源对象类型,一定要继承自 QObject
class MySignals(QObject):
# 还可以定义其他种类的信号
# 类型必须定义
update_table = Signal(str)
show_message = Signal(str)
# 实例化
global_ms = MySignals()
class Stats:
def __init__(self):
self.ui = QUiLoader().load('ui/main.ui') # 加载UI文件
self.ui.setFixedSize(self.ui.width(),self.ui.height()) # 固定界面大小
# 自定义信号的处理函数
global_ms.update_table.connect(self.printToGui) # 信号处理
global_ms.show_message.connect(self.show_ms)
self.ui.pushButton.clicked.connect(self.task1)
self.ui.pushButton_2.clicked.connect(self.task2)
def printToGui(self,text):
self.ui.textBrowser.append(text)
def show_ms(self, text):
QMessageBox.information(self.ui, text, text)
def task1(self):
def threadFunc():
# 通过Signal 的 emit 触发执行 主线程里面的处理函数
# emit参数和定义Signal的数量、类型必须一致
r = requests.get('https://www.sengshao.com')
r.encoding = 'utf-8'
title = re.search('<title>(.*?)</title>', r.text).group(1)
print(title)
global_ms.update_table.emit(title)
global_ms.show_message.emit(title)
thread = Thread(target = threadFunc )
thread.start()
def task2(self):
def threadFunc():
r = requests.get('http://www.baidu.com')
r.encoding = 'utf-8'
title = re.search('<title>(.*?)</title>', r.text).group(1)
print(title)
global_ms.update_table.emit(title)
thread = Thread(target=threadFunc)
thread.start()
if __name__ == '__main__':
app = QApplication([])
stats = Stats()
stats.ui.show()
app.exec()上一篇:投资 -- 房子 + 股票
下一篇:做项目必须的几道门槛
发表评论