我正在尝试我的第一个程序,并希望有人能告诉我出错了什么。我写了这个程序弹出一个有4个选项的屏幕,当我点击其中一个选项时它应该将屏幕切换到下一个屏幕,请建议我写错了什么因为我得到的所有是第一个屏幕然后没有什么时候我点击按钮谢谢你。
这是程序
import tkinter as tk
LARGE_FONT = ("Verdana", 12)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
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.grid(row=0, column=0, sticky="nsew")
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)
btn1 = tk.Button(text="", fg="white", width=400, height=240, command=lambda : controller.show_frame(PageOne))
btn1["bg"] = "white"
mi = tk.PhotoImage(file="C:\\Python\\trials\\pic1.gif")
btn1.config(image=mi)
btn1.image = mi
btn2 = tk.Button(text="", fg="white", width=400, height=240, command=lambda: controller.show_frame(PageTwo))
btn2["bg"] = "white"
mi1 = tk.PhotoImage(file="C:\\Python\\trials\\safety.gif")
btn2.config(image=mi1)
btn2.image = mi1
btn3 = tk.Button(text="", fg="white", width=400, height=240)
btn3["bg"] = "white"
mi2 = tk.PhotoImage(file="C:\\Python\\trials\\count.gif")
btn3.config(image=mi2)
btn3.image = mi2
btn4 = tk.Button(text="", fg="white", width=400, height=240)
btn4["bg"] = "white"
mi3 = tk.PhotoImage(file="C:\\Python\\trials\\about.gif")
btn4.config(image=mi3)
btn4.image = mi3
btn1.grid(row=0, column=0, columnspan=1, sticky='EW')
btn2.grid(row=0, column=1, columnspan=1, sticky='EW')
btn3.grid(row=1, column=0, columnspan=1, sticky='EW')
btn4.grid(row=1, column=1, columnspan=1, sticky='Ew')
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)
label.grid(pady=10, padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.grid()
button2 = tk.Button(self, text="Page Two",
command=lambda: controller.show_frame(PageTwo))
button2.grid()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT)
label.grid(pady=10, padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.grid()
button2 = tk.Button(self, text="Page One",
command=lambda: controller.show_frame(PageOne))
button2.grid()
app = SeaofBTCapp()
app.mainloop()
答案 0 :(得分:0)
你有两个问题:
首先,你的所有按钮都需要第一个参数'self'。您在某些地方执行此操作但在StartPage类中忘记了它。
其次,您需要布置容器。初始化后添加pack()
:
container = tk.Frame(self)
container.pack()