python tkinter 界面上面是按钮,通过选择按钮下面切换至不同的界面,界面包含不同的按钮,通过操作按钮可以执行不同的程序,实现代码
时间: 2024-05-05 21:19:23 浏览: 125
这里是一个简单的示例代码,可以实现在 tkinter 界面中点击不同的按钮来切换不同的页面,每个页面包含不同的按钮,点击按钮可以执行不同的程序。
```
import tkinter as tk
class Page:
def __init__(self, parent):
self.frame = tk.Frame(parent)
self.frame.pack()
class MainPage(Page):
def __init__(self, parent):
super().__init__(parent)
self.button1 = tk.Button(self.frame, text="Page 1", command=lambda: parent.show_page(Page1))
self.button1.pack()
self.button2 = tk.Button(self.frame, text="Page 2", command=lambda: parent.show_page(Page2))
self.button2.pack()
class Page1(Page):
def __init__(self, parent):
super().__init__(parent)
self.button1 = tk.Button(self.frame, text="Run Program 1", command=self.run_program1)
self.button1.pack()
self.button2 = tk.Button(self.frame, text="Back", command=lambda: parent.show_page(MainPage))
self.button2.pack()
def run_program1(self):
# code to run program 1
pass
class Page2(Page):
def __init__(self, parent):
super().__init__(parent)
self.button1 = tk.Button(self.frame, text="Run Program 2", command=self.run_program2)
self.button1.pack()
self.button2 = tk.Button(self.frame, text="Back", command=lambda: parent.show_page(MainPage))
self.button2.pack()
def run_program2(self):
# code to run program 2
pass
class App(tk.Tk):
def __init__(self):
super().__init__()
self.pages = {}
for page in (MainPage, Page1, Page2):
self.pages[page] = page(self)
self.show_page(MainPage)
def show_page(self, page):
self.pages[page].frame.tkraise()
app = App()
app.mainloop()
```
阅读全文