我正在创建一个非常简单的GUI,每个帧都应该有一个Continue和Exit按钮。我需要继续按钮来打开一个带有书写,小部件等的新框架。
第一帧(frame1
)在我调用frame_2
函数时起作用
但是由此我不知道如何打开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()
答案 0 :(得分:0)
你的代码有点搞砸了。 在下面找到一个工作示例。 在发布任何其他问题之前,请确保您的代码是“可运行的”,如果您正在努力解决逻辑问题或发布错误消息(在python控制台中打印错误)。
请确保缩进正确。
当我使用python 2.7时,您需要将Tkinter
替换为tkinter
。
可能需要再次从BOTH
导入tkinter.constants
。
您的代码出了什么问题?
frame_2().grid.forget()
- 它有什么问题?您需要在函数调用(frame_2()
)而不是对象(frame2
)上调用某些内容。此对象也未被声明为全局,因此没有机会获取您想要的对象但是全局声明它。 (包括初始化和使用)
这条线还有什么不对?该函数在作为“函数调用对象”一部分的网格对象上被称为grid_forget
而不是forget
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()
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()