不确定我的代码有什么问题(Tkinter BEGINNER)
Not sure what is wrong with my code (Tkinter BEGINNER)
我正在制作一个非常简单的 GUI,每一帧都应该有一个继续和退出按钮。我需要继续按钮来打开一个新框架,上面有文字、小部件等。
当我调用 frame_2
函数时,第一帧 (frame1
) 有效。
但由此我不知道如何正确打开 frame_3
和销毁 frame 2
。
目前的代码如下:
import tkinter
from tkinter.constants import *
tk = tkinter.Tk()
def frame_2(): #ENTERING AGE
frame1.grid_forget()
frame1.destroy()
frame2 = tkinter.Frame(tk, borderwidth=2,)
frame2.pack(fill=BOTH,expand=1,pady=50,padx=80)
need_info = tkinter.Label(frame2, text="I need some information first...")
need_info.grid(row=0, column=0) #displays text at top of frame
enter_age = tkinter.Label(frame2, text="Please enter your age!")
enter_age.grid(row=2, column=0) #displays second line of text
age = tkinter.Entry(frame2, width=10)
age.grid(row=3, column=0)
nextpage = tkinter.Button(frame2,text="Continue",command=frame2.destroy)
nextpage.grid(row=10, column=0)
exitapp = tkinter.Button(frame2,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=12, column=0)
def frame_three(): #ENTERING EDUCATION
frame_2().grid.forget()
frame_2().destroy()
frame3 = tkinter.Frame(tk, borderwidth=2)
frame3.pack(fill=BOTH,expand=1,pady=50,padx=80)
def frame_3(): #ENTERING EDUCATION
frame_2().grid.forget()
frame_2().destroy()
frame3 = tkinter.Frame(tk, borderwidth=2)
frame3.pack(fill=BOTH,expand=1,pady=50,padx=80)
age_confirm = tkinter.Label(frame3, text="You entered 38!")
age_confirm.grid(row=0, column=0)
nextpage.grid(row=10, column=0)
exitapp = tkinter.Button(frame2,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=12, column=0)
frame1 = tkinter.Frame(tk, borderwidth=2) #WELCOME PAGE, a.k.a The first frame
frame1.pack(fill=BOTH,expand=1,pady=50,padx=80)
label = tkinter.Label(frame1, text="Welcome to NAME GAME! I'm going to guess who you are...")
label.grid(row=0, column=0) #displays text at top of frame
nextpage = tkinter.Button(frame1,text="Continue",command=frame_2)
def frame_2(): #ENTERING AGE
frame1.grid_forget()
frame1.destroy()
frame2 = tkinter.Frame(tk, borderwidth=2,)
frame2.pack(fill=BOTH,expand=1,pady=50,padx=80)
need_info = tkinter.Label(frame2, text="I need some information first...")
need_info.grid(row=0, column=0) #displays text at top of frame
enter_age = tkinter.Label(frame2, text="Please enter your age!")
enter_age.grid(row=2, column=0) #displays second line of text
age = tkinter.Entry(frame2, width=10)
age.grid(row=3, column=0)
nextpage = tkinter.Button(frame2,text="Continue",command=frame_3)
nextpage.grid(row=10, column=0)
exitapp = tkinter.Button(frame2,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=12, column=0)
nextpage.grid(row=2, column=0)
exitapp = tkinter.Button(frame1,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=4, column=0)
tk.mainloop()
你的代码有点乱。
在下面找到一个工作示例。
在您 post 任何其他问题之前,请确保您的代码是 "runable",如果您遇到逻辑问题或 post 您的错误消息(打印在 python 控制台中的错误).
另请确保您的缩进正确无误。
由于我使用的是 python 2.7,因此您需要将 Tkinter
替换为 tkinter
。
可能是您需要再次从 tkinter.constants
导入 BOTH
。
你的代码有什么问题?
- 您对对象和函数使用了相似的名称。这样做的结果是您尝试了
frame_2().grid.forget()
- 这有什么问题吗?您需要在函数调用 (frame_2()
) 而不是对象 (frame2
) 上调用某些内容。该对象也未声明为全局对象,因此没有机会获取您想要的对象,而是将其声明为全局对象。 (包括初始化和使用)
那条线还有什么问题?该函数在属于 "function call object" 的网格对象上被称为 grid_forget
而不是 forget
- 第二件事:你对两个不同的函数使用了相同的函数名。 不要那样做。只是不要。
</p>
<pre><code>import Tkinter
tk = Tkinter.Tk()
def frame_2():
"""
ENTERING AGE
"""
global frame1
global frame2
if frame1 != None:
frame1.grid_forget()
frame1.destroy()
frame1=None
frame2 = Tkinter.Frame(tk, borderwidth=2,)
frame2.pack(fill=Tkinter.BOTH,expand=1,pady=50,padx=80)
need_info = Tkinter.Label(frame2, text="I need some information first...")
need_info.grid(row=0, column=0) #displays text at top of frame
enter_age = Tkinter.Label(frame2, text="Please enter your age!")
enter_age.grid(row=2, column=0) #displays second line of text
age = Tkinter.Entry(frame2, width=10)
age.grid(row=3, column=0)
nextpage = Tkinter.Button(frame2,text="Continue",command=frame_3)
nextpage.grid(row=10, column=0)
exitapp = Tkinter.Button(frame2,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=12, column=0)
def frame_3(): #ENTERING EDUCATION
global frame2
global frame3
if frame2!=None:
frame2.grid_forget()
frame2.destroy()
frame2=None
frame3 = Tkinter.Frame(tk, borderwidth=2)
frame3.pack(fill=Tkinter.BOTH,expand=1,pady=50,padx=80)
age_confirm = Tkinter.Label(frame3, text="You entered 38!")
age_confirm.grid(row=0, column=0)
exitapp = Tkinter.Button(frame3,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=12, column=0)
frame2=None
frame3=None
frame1 = Tkinter.Frame(tk, borderwidth=2) #WELCOME PAGE, a.k.a The first frame
frame1.pack(fill=Tkinter.BOTH,expand=1,pady=50,padx=80)
label = Tkinter.Label(frame1, text="Welcome to NAME GAME! I'm going to guess who you are...")
label.grid(row=0, column=0) #displays text at top of frame
nextpage = Tkinter.Button(frame1,text="Continue",command=frame_2)
nextpage.grid(row=2, column=0)
exitapp = Tkinter.Button(frame1,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=4, column=0)
tk.mainloop()
我正在制作一个非常简单的 GUI,每一帧都应该有一个继续和退出按钮。我需要继续按钮来打开一个新框架,上面有文字、小部件等。
当我调用 frame_2
函数时,第一帧 (frame1
) 有效。
但由此我不知道如何正确打开 frame_3
和销毁 frame 2
。
目前的代码如下:
import tkinter
from tkinter.constants import *
tk = tkinter.Tk()
def frame_2(): #ENTERING AGE
frame1.grid_forget()
frame1.destroy()
frame2 = tkinter.Frame(tk, borderwidth=2,)
frame2.pack(fill=BOTH,expand=1,pady=50,padx=80)
need_info = tkinter.Label(frame2, text="I need some information first...")
need_info.grid(row=0, column=0) #displays text at top of frame
enter_age = tkinter.Label(frame2, text="Please enter your age!")
enter_age.grid(row=2, column=0) #displays second line of text
age = tkinter.Entry(frame2, width=10)
age.grid(row=3, column=0)
nextpage = tkinter.Button(frame2,text="Continue",command=frame2.destroy)
nextpage.grid(row=10, column=0)
exitapp = tkinter.Button(frame2,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=12, column=0)
def frame_three(): #ENTERING EDUCATION
frame_2().grid.forget()
frame_2().destroy()
frame3 = tkinter.Frame(tk, borderwidth=2)
frame3.pack(fill=BOTH,expand=1,pady=50,padx=80)
def frame_3(): #ENTERING EDUCATION
frame_2().grid.forget()
frame_2().destroy()
frame3 = tkinter.Frame(tk, borderwidth=2)
frame3.pack(fill=BOTH,expand=1,pady=50,padx=80)
age_confirm = tkinter.Label(frame3, text="You entered 38!")
age_confirm.grid(row=0, column=0)
nextpage.grid(row=10, column=0)
exitapp = tkinter.Button(frame2,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=12, column=0)
frame1 = tkinter.Frame(tk, borderwidth=2) #WELCOME PAGE, a.k.a The first frame
frame1.pack(fill=BOTH,expand=1,pady=50,padx=80)
label = tkinter.Label(frame1, text="Welcome to NAME GAME! I'm going to guess who you are...")
label.grid(row=0, column=0) #displays text at top of frame
nextpage = tkinter.Button(frame1,text="Continue",command=frame_2)
def frame_2(): #ENTERING AGE
frame1.grid_forget()
frame1.destroy()
frame2 = tkinter.Frame(tk, borderwidth=2,)
frame2.pack(fill=BOTH,expand=1,pady=50,padx=80)
need_info = tkinter.Label(frame2, text="I need some information first...")
need_info.grid(row=0, column=0) #displays text at top of frame
enter_age = tkinter.Label(frame2, text="Please enter your age!")
enter_age.grid(row=2, column=0) #displays second line of text
age = tkinter.Entry(frame2, width=10)
age.grid(row=3, column=0)
nextpage = tkinter.Button(frame2,text="Continue",command=frame_3)
nextpage.grid(row=10, column=0)
exitapp = tkinter.Button(frame2,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=12, column=0)
nextpage.grid(row=2, column=0)
exitapp = tkinter.Button(frame1,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=4, column=0)
tk.mainloop()
你的代码有点乱。 在下面找到一个工作示例。 在您 post 任何其他问题之前,请确保您的代码是 "runable",如果您遇到逻辑问题或 post 您的错误消息(打印在 python 控制台中的错误).
另请确保您的缩进正确无误。
由于我使用的是 python 2.7,因此您需要将 Tkinter
替换为 tkinter
。
可能是您需要再次从 tkinter.constants
导入 BOTH
。
你的代码有什么问题?
- 您对对象和函数使用了相似的名称。这样做的结果是您尝试了
frame_2().grid.forget()
- 这有什么问题吗?您需要在函数调用 (frame_2()
) 而不是对象 (frame2
) 上调用某些内容。该对象也未声明为全局对象,因此没有机会获取您想要的对象,而是将其声明为全局对象。 (包括初始化和使用) 那条线还有什么问题?该函数在属于 "function call object" 的网格对象上被称为 - 第二件事:你对两个不同的函数使用了相同的函数名。 不要那样做。只是不要。
grid_forget
而不是 forget
</p>
<pre><code>import Tkinter
tk = Tkinter.Tk()
def frame_2():
"""
ENTERING AGE
"""
global frame1
global frame2
if frame1 != None:
frame1.grid_forget()
frame1.destroy()
frame1=None
frame2 = Tkinter.Frame(tk, borderwidth=2,)
frame2.pack(fill=Tkinter.BOTH,expand=1,pady=50,padx=80)
need_info = Tkinter.Label(frame2, text="I need some information first...")
need_info.grid(row=0, column=0) #displays text at top of frame
enter_age = Tkinter.Label(frame2, text="Please enter your age!")
enter_age.grid(row=2, column=0) #displays second line of text
age = Tkinter.Entry(frame2, width=10)
age.grid(row=3, column=0)
nextpage = Tkinter.Button(frame2,text="Continue",command=frame_3)
nextpage.grid(row=10, column=0)
exitapp = Tkinter.Button(frame2,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=12, column=0)
def frame_3(): #ENTERING EDUCATION
global frame2
global frame3
if frame2!=None:
frame2.grid_forget()
frame2.destroy()
frame2=None
frame3 = Tkinter.Frame(tk, borderwidth=2)
frame3.pack(fill=Tkinter.BOTH,expand=1,pady=50,padx=80)
age_confirm = Tkinter.Label(frame3, text="You entered 38!")
age_confirm.grid(row=0, column=0)
exitapp = Tkinter.Button(frame3,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=12, column=0)
frame2=None
frame3=None
frame1 = Tkinter.Frame(tk, borderwidth=2) #WELCOME PAGE, a.k.a The first frame
frame1.pack(fill=Tkinter.BOTH,expand=1,pady=50,padx=80)
label = Tkinter.Label(frame1, text="Welcome to NAME GAME! I'm going to guess who you are...")
label.grid(row=0, column=0) #displays text at top of frame
nextpage = Tkinter.Button(frame1,text="Continue",command=frame_2)
nextpage.grid(row=2, column=0)
exitapp = Tkinter.Button(frame1,text="Exit",command=tk.destroy) #exits programme
exitapp.grid(row=4, column=0)
tk.mainloop()