您好
我真的坚持使用这个python代码。也许你可以帮助我?
我想用按钮导航页面。我有三个不同的框架。然后我按下一个按钮,一个框架在其他框架上,但不要隐藏其他框架。
如果我的问题不清楚,只需尝试我的代码,您就会明白我的意思。
这段代码很少修改。
import tkinter as tk
from tkinter import ttk
from tkinter import StringVar
LARGE_FONT= ("Verdana", 12)
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Title")
container = tk.Frame(self, width=768, height=1000)
container.pack(side="top", fill='both' , expand = 1)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
#frame.pack()
frame.grid(row=0, column=0)
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = ttk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = ttk.Button(self, text="Visit Page 1",
command=lambda: controller.show_frame(PageOne))
button.pack()
button2 = ttk.Button(self, text="Visit Page 2",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text="Page One!!!", font=LARGE_FONT)
label.pack(pady=0,padx=100)
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = ttk.Button(self, text="Page Two",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text="Page Two!!!", font=LARGE_FONT)
label.pack(pady=0,padx=0)
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = ttk.Button(self, text="Page One",
command=lambda: controller.show_frame(PageOne))
button2.pack()
phone = StringVar()
home = ttk.Radiobutton(self, text='Home', variable=phone, value='home')
office = ttk.Radiobutton(self, text='Office', variable=phone, value='office')
cell = ttk.Radiobutton(self, text='Mobile', variable=phone, value='cell')
home.pack()
office.pack()
cell.pack()
app = Application()
app.mainloop()
非常感谢:)
答案 0 :(得分:1)
您需要告诉所有帧占用所有可用空间,以便它们覆盖它们下面的任何帧。您可以使用粘性参数:
frame.grid(row=0, column=0, sticky='nsew')
告诉框架坚持北,南,东,西两侧;换句话说,填充两个方向的所有空间。