我正在努力制作一个简单的Magic Eight Ball应用程序,仅用于练习。我正在使用tkinter和python 3.0。我正在尝试将帧切换到应用程序的下一部分但无法解决它。我只是想知道这个代码有什么问题,因为每次我尝试纠正它时,它只会出现错误“容器未定义”或“PageOne没有属性'tk'”。我想说到目前为止我对tkinter很满意,并且可以制作只有一页的基本GUI。但是,我似乎无法工作。这是代码:
import tkinter as tk
class EightBall(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args,**kwargs)
container = tk.Frame(self)
container.configure(width=500, height=500)
container.pack()
self.frames={}
for F in (StartPage, PageOne):
frame = F(container,self)
self.frames[F]=frame
frame.configure(width=500,height=500)
frame.pack()
self.show_frame(StartPage)
def show_frame(self,controller):
frame=self.frames[container]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
HomeButton=tk.Button(self, text="Press to play",
command=lambda: controller.show_frame(PageOne))
HomeLabel=tk.Label(self, text="Magic 8 Ball", fg="white",bg="blue")
HomeLabel.configure(font="Arial 22")
HomeButton.place(relx=0.5,rely=0.5,anchor="center")
HomeLabel.place(relx=0.35,y=10)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
PageOneLabel=tk.Label(PageOne,text="What is your name?")
PageOneEntry=Entry(PageOne)
PageOneButton=tk.Button(PageOne, text="Enter",
command=lambda: controller.show_frame(PageTwo))
PageOneLabel.place(relx=0.50,rely=0.45)
PageOneEntry.place(relx=0.50,rely=0.50)
PageOneButton.place(relx=0.50,rely=0.55)
app=EightBall()
app.mainloop()
我做错了什么?
答案 0 :(得分:0)
工作代码:
uri: https://github.com/pivotal-cf/pcf-pipelines
修正:
在import tkinter as tk
class EightBall(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args,**kwargs)
self.container = tk.Frame(self)
self.container.pack()
self.frames={}
for F in (StartPage, PageOne):
frame = F(self.container, self)
self.frames[F]=frame
frame.configure(width=500, height=500)
frame.grid(row=0, column=0)
self.show_frame(StartPage)
def show_frame(self, controller):
frame=self.frames[controller]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
HomeButton=tk.Button(self, text="Press to play",
command=lambda: controller.show_frame(PageOne))
HomeLabel=tk.Label(self, text="Magic 8 Ball", fg="white",bg="blue")
HomeLabel.configure(font="Arial 22")
HomeButton.place(relx=0.5,rely=0.5,anchor="center")
HomeLabel.place(relx=0.35,y=10)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
PageOneLabel=tk.Label(self,text="What is your name?")
PageOneEntry=tk.Entry(self)
PageOneButton=tk.Button(self, text="Enter",
command=lambda: controller.show_frame(StartPage))
PageOneLabel.place(relx=0.50,rely=0.45)
PageOneEntry.place(relx=0.50,rely=0.50)
PageOneButton.place(relx=0.50,rely=0.55)
app=EightBall() ; app.mainloop()
将EightBall.__init__
替换为PageTwo
,因为其名称为
将StartPage
替换为container
中的controller
,因为那已经过了
将EightBall.show_frame
替换为pack()
中的grid(row=0, column=0)
,以便将帧放在彼此之上
在EightBall.__init__
tk.
之前添加了Entry
删除空白行,加上最后两行,以便代码无需滚动即可。