用Python设置UI互动界面,设置5个按钮,每个按钮对应独立的程序,利用pyqt实现
时间: 2024-02-28 12:56:29 浏览: 31
使用PyQt库可以在Python中设置UI互动界面,并为每个按钮定义单独的程序。以下是一个使用PyQt实现的示例程序:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtCore import QCoreApplication
import subprocess
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# Button 1
self.button1 = QPushButton('Program 1', self)
self.button1.move(20, 20)
self.button1.clicked.connect(self.program1)
# Button 2
self.button2 = QPushButton('Program 2', self)
self.button2.move(20, 60)
self.button2.clicked.connect(self.program2)
# Button 3
self.button3 = QPushButton('Program 3', self)
self.button3.move(20, 100)
self.button3.clicked.connect(self.program3)
# Button 4
self.button4 = QPushButton('Program 4', self)
self.button4.move(20, 140)
self.button4.clicked.connect(self.program4)
# Button 5
self.button5 = QPushButton('Program 5', self)
self.button5.move(20, 180)
self.button5.clicked.connect(self.program5)
# Quit Button
qbtn = QPushButton('Quit', self)
qbtn.clicked.connect(QCoreApplication.instance().quit)
qbtn.resize(qbtn.sizeHint())
qbtn.move(20, 220)
# Window Settings
self.setGeometry(300, 300, 250, 300)
self.setWindowTitle('Program Selector')
self.show()
# Program 1
def program1(self):
subprocess.call(["python", "program1.py"])
# Program 2
def program2(self):
subprocess.call(["python", "program2.py"])
# Program 3
def program3(self):
subprocess.call(["python", "program3.py"])
# Program 4
def program4(self):
subprocess.call(["python", "program4.py"])
# Program 5
def program5(self):
subprocess.call(["python", "program5.py"])
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
在这个示例程序中,我们使用了PyQt库创建了一个窗口,并在窗口中放置了5个按钮和一个退出按钮。每个按钮都有一个对应的程序,当用户点击按钮时,程序会调用对应的程序。你需要将`program1.py`、`program2.py`、`program3.py`、`program4.py`和`program5.py`替换为你自己的程序文件名。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)