繁体   English   中英

不确定我的代码有什么问题(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()

你的代码有点乱。 在下面找到一个工作示例。 在发布任何其他问题之前,如果您遇到逻辑问题或发布错误消息(在 python 控制台中打印的错误),请确保您的代码是“可运行的”。

还请确保您的缩进是正确的。

由于我使用Python 2.7,你将需要更换Tkintertkinter 可能是您需要再次从tkinter.constants导入BOTH

你的代码有什么问题?

  • 您对对象和函数使用了相似的名称。 这样做的原因是您尝试了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, aka 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM