使用 Tkinter 中的按钮导航到应用程序的不同页面?
- 2025-03-04 08:24:00
- admin 原创
- 65
问题描述:
我这里有一个非常简单的问题。在 Tkinter (python) 中,我想知道如何使用按钮转到应用程序的不同页面,例如注册页面和登录页面。我知道 GUI 没有像网站那样的“页面”,我见过几种不同的方法,但创建指向不同页面的链接的最佳方法是什么?
解决方案 1:
将每个页面设为一个框架。然后,您的所有按钮需要做的就是隐藏可见的内容,然后使所需的框架可见。
一个简单的方法是将框架堆叠在一起(这是有意义的place
),然后显示lift()
您希望可见的框架。当所有页面的大小相同时,此技术效果最佳;事实上,它要求您明确设置包含框架的大小。
以下是一个虚构的例子。这不是解决问题的唯一方法,只是证明这不是一个特别难解决的问题:
import Tkinter as tk
class Page(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
def show(self):
self.lift()
class Page1(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
label = tk.Label(self, text="This is page 1")
label.pack(side="top", fill="both", expand=True)
class Page2(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
label = tk.Label(self, text="This is page 2")
label.pack(side="top", fill="both", expand=True)
class Page3(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
label = tk.Label(self, text="This is page 3")
label.pack(side="top", fill="both", expand=True)
class MainView(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
p1 = Page1(self)
p2 = Page2(self)
p3 = Page3(self)
buttonframe = tk.Frame(self)
container = tk.Frame(self)
buttonframe.pack(side="top", fill="x", expand=False)
container.pack(side="top", fill="both", expand=True)
p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
p3.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
b1 = tk.Button(buttonframe, text="Page 1", command=p1.show)
b2 = tk.Button(buttonframe, text="Page 2", command=p2.show)
b3 = tk.Button(buttonframe, text="Page 3", command=p3.show)
b1.pack(side="left")
b2.pack(side="left")
b3.pack(side="left")
p1.show()
if __name__ == "__main__":
root = tk.Tk()
main = MainView(root)
main.pack(side="top", fill="both", expand=True)
root.wm_geometry("400x400")
root.mainloop()
解决方案 2:
你能做这样的事吗?
import tkinter
def page1():
page2text.pack_forget()
page1text.pack()
def page2():
page1text.pack_forget()
page2text.pack()
window = tkinter.Tk()
page1btn = tkinter.Button(window, text="Page 1", command=page1)
page2btn = tkinter.Button(window, text="Page 2", command=page2)
page1text = tkinter.Label(window, text="This is page 1")
page2text = tkinter.Label(window, text="This is page 2")
page1btn.pack()
page2btn.pack()
page1text.pack()
对我来说这似乎简单多了。
解决方案 3:
import tkinter as tk
root=tk.Tk()
root.geometry("360x360")
frame=tk.Frame(root,bg='lightblue')
frame.place(relx=0.2,rely=0.2,relheight=0.6,relwidth=0.6)
def page1():
label=tk.Label(frame,text='this is the page1')
label.place(relx=0.3,rely=0.4)
def page2():
label=tk.Label(frame,text='this is the page2')
label.place(relx=0.3,rely=0.4)
def page3():
label=tk.Label(frame,text='this is the page3')
label.place(relx=0.3,rely=0.4)
bt=tk.Button(root,text='page1',command=page1)
bt.grid(column=0,row=0)
bt1=tk.Button(root,text='page2',command=page2)
bt1.grid(row=0,column=1)
bt2=tk.Button(root,text='page3',command=page3)
bt2.grid(row=0,column=2)
root.mainloop()`
相关推荐
热门文章
项目管理软件有哪些?
热门标签
曾咪二维码
扫码咨询,免费领取项目管理大礼包!
云禅道AD