用Python设置UI互动界面,设置5个按钮,每个按钮对应独立的程序
时间: 2024-02-28 18:56:27 浏览: 141
可以使用Tkinter库在Python中设置UI互动界面,并为每个按钮定义单独的程序。以下是一个示例程序:
```python
import tkinter as tk
import subprocess
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
# Button 1
self.button1 = tk.Button(self, text="Program 1", command=self.program1)
self.button1.pack(side="top")
# Button 2
self.button2 = tk.Button(self, text="Program 2", command=self.program2)
self.button2.pack(side="top")
# Button 3
self.button3 = tk.Button(self, text="Program 3", command=self.program3)
self.button3.pack(side="top")
# Button 4
self.button4 = tk.Button(self, text="Program 4", command=self.program4)
self.button4.pack(side="top")
# Button 5
self.button5 = tk.Button(self, text="Program 5", command=self.program5)
self.button5.pack(side="top")
# Quit Button
self.quit = tk.Button(self, text="QUIT", fg="red", command=self.master.destroy)
self.quit.pack(side="bottom")
# 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"])
root = tk.Tk()
app = Application(master=root)
app.mainloop()
```
在这个示例程序中,我们使用了Tkinter库创建了一个窗口,并在窗口中放置了5个按钮和一个退出按钮。每个按钮都有一个对应的程序,当用户点击按钮时,程序会调用对应的程序。你需要将`program1.py`、`program2.py`、`program3.py`、`program4.py`和`program5.py`替换为你自己的程序文件名。
阅读全文