import tkinter as tk
class App:
def __init__(self, root):
self.root = root
self.root.title("导航栏与局部页面切换")
self.root.geometry('800x500')
self.root.minsize(width=800, height=300)
# 创建导航栏
self.navbar = tk.Frame(self.root, bg='gray')
self.navbar.pack(fill=tk.BOTH, side=tk.TOP)
# 导航栏按钮
self.btn_page1 = tk.Button(self.navbar, bg='#ffffff', fg='#000000', text="页面1", command=self.show_page1)
self.btn_page2 = tk.Button(self.navbar, bg='#ffffff', fg='#000000', text="页面2", command=self.show_page2)
self.btn_page3 = tk.Button(self.navbar, bg='#ffffff', fg='#000000', text="页面3", command=self.show_page3)
self.btn_page4 = tk.Button(self.navbar, bg='#ffffff', fg='#000000', text="页面4", command=self.show_page4)
# 设置一行上的4个列,weight 是长度权重,这里设置为权重一样就是等长
self.navbar.columnconfigure(0, weight=1)
self.navbar.columnconfigure(1, weight=1)
self.navbar.columnconfigure(2, weight=1)
self.navbar.columnconfigure(3, weight=1)
# row 行号,column 列号
# sticky 指定了当网格的大小改变时,按钮将如何对齐。
# "ew"代表"east and west",
# 意味着按钮将填充其分配的行和列空间,从东(右)和西(左)两个方向填充。
self.btn_page1.grid(row=0, column=0, sticky="ew")
self.btn_page2.grid(row=0, column=1, sticky="ew")
self.btn_page3.grid(row=0, column=2, sticky="ew")
self.btn_page4.grid(row=0, column=3, sticky="ew")
# 局部页面容器
self.content_frame = tk.Frame(self.root)
self.content_frame.pack(fill=tk.BOTH, expand=True)
# 页面内容
self.page1 = tk.Frame(self.content_frame, bg='#ffffff')
self.page2 = tk.Frame(self.content_frame, bg='#ffffff')
self.page3 = tk.Frame(self.content_frame, bg='#ffffff')
self.page4 = tk.Frame(self.content_frame, bg='#ffffff')
# 自己加的标签
self.label1 = tk.Label(self.page1, text="页面1", bg='yellow', fg='#000000', font=('微软雅黑', 16)).pack()
self.label2 = tk.Label(self.page2, text="页面2", bg='yellow', fg='#000000', font=('微软雅黑', 16)).pack()
self.label3 = tk.Label(self.page3, text="页面3", bg='yellow', fg='#000000', font=('微软雅黑', 16)).pack()
self.label4 = tk.Label(self.page4, text="页面4", bg='yellow', fg='#000000', font=('微软雅黑', 16)).pack()
# 初始显示页面1
self.show_page1()
def show_page1(self):
self.page1.pack(fill=tk.BOTH, expand=True)
self.btn_page1.config(fg='red')
self.btn_page2.config(fg='#000000')
self.btn_page3.config(fg='#000000')
self.btn_page4.config(fg='#000000')
self.page2.pack_forget() # 隐藏页面2
self.page3.pack_forget()
self.page4.pack_forget()
def show_page2(self):
self.page2.pack(fill=tk.BOTH, expand=True)
self.btn_page2.config(fg='red')
self.btn_page1.config(fg='#000000')
self.btn_page3.config(fg='#000000')
self.btn_page4.config(fg='#000000')
self.page1.pack_forget() # 隐藏页面1
self.page3.pack_forget()
self.page4.pack_forget()
def show_page3(self):
self.page3.pack(fill=tk.BOTH, expand=True)
self.btn_page3.config(fg='red')
self.btn_page1.config(fg='#000000')
self.btn_page2.config(fg='#000000')
self.btn_page4.config(fg='#000000')
self.page1.pack_forget() # 隐藏页面1
self.page2.pack_forget()
self.page4.pack_forget()
def show_page4(self):
self.page4.pack(fill=tk.BOTH, expand=True)
self.btn_page4.config(fg='red')
self.btn_page1.config(fg='#000000')
self.btn_page2.config(fg='#000000')
self.btn_page3.config(fg='#000000')
self.page1.pack_forget() # 隐藏页面1
self.page3.pack_forget()
self.page2.pack_forget()
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
效果展示: