pyqt中通过两个按钮利用线程分别启动功能包(python)
时间: 2023-12-06 22:01:55 浏览: 140
可以使用Python中的QThread类来创建线程,并通过QPushButton类中的clicked信号将按钮点击事件与线程的启动函数连接起来。在启动线程时,可以使用Python中的os模块来启动功能包。以下是一个示例代码:
```python
import os
from PyQt5.QtCore import QThread
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
class FunctionThread(QThread):
def __init__(self, function_path):
super().__init__()
self.function_path = function_path
def run(self):
os.system("python " + self.function_path)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("启动功能包")
self.setGeometry(100, 100, 300, 200)
self.button1 = QPushButton("启动功能包1", self)
self.button1.setGeometry(50, 50, 200, 30)
self.button2 = QPushButton("启动功能包2", self)
self.button2.setGeometry(50, 100, 200, 30)
self.button1.clicked.connect(self.start_function1_thread)
self.button2.clicked.connect(self.start_function2_thread)
def start_function1_thread(self):
function_path = "function1.py"
function_thread = FunctionThread(function_path)
function_thread.start()
def start_function2_thread(self):
function_path = "function2.py"
function_thread = FunctionThread(function_path)
function_thread.start()
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
在此示例代码中,我们定义了一个FunctionThread类,用于创建线程并启动功能包。在MainWindow类中,我们创建了两个QPushButton对象,并将它们的clicked信号分别连接到start_function1_thread()和start_function2_thread()槽函数。这些槽函数中,我们创建了FunctionThread对象,并传入要启动的功能包路径。然后,我们调用start()函数来启动线程。
阅读全文