简体   繁体   English

不确定我的代码有什么问题(Tkinter BEGINNER)

[英]Not sure what is wrong with my code (Tkinter BEGINNER)

I'm making a pretty simple GUI and each frame should have a Continue and Exit button.我正在制作一个非常简单的 GUI,每一帧都应该有一个继续和退出按钮。 I need the continue button to open a new frame with writing, widgets...etc on it.我需要继续按钮来打开一个带有文字、小部件...等的新框架。

The first frame ( frame1 ) works when I call the frame_2 function.当我调用frame_2函数时,第一帧 ( frame1 ) 起作用。
But from this I do not know how to open a frame_3 and destroy frame 2 properly.但是由此我不知道如何打开frame_3并正确销毁frame 2

Here is the code so far:这是到目前为止的代码:

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()

Your code is a bit messed up.你的代码有点乱。 Find below a working example.在下面找到一个工作示例。 Before you post any other question, make sure that your code is "runable", if you are struggling with logical issues or post your error messages (the errors printed in python console).在发布任何其他问题之前,如果您遇到逻辑问题或发布错误消息(在 python 控制台中打印的错误),请确保您的代码是“可运行的”。

Please also make sure that your indentation is correct.还请确保您的缩进是正确的。

As I am using python 2.7 you would need to replace Tkinter by tkinter .由于我使用Python 2.7,你将需要更换Tkintertkinter May be that you need to import BOTH from tkinter.constants again.可能是您需要再次从tkinter.constants导入BOTH

What was wrong in your code?你的代码有什么问题?

  • You used similar names for objects and functions.您对对象和函数使用了相似的名称。 What came around doing this was that you tried frame_2().grid.forget() - what is wrong about it?这样做的原因是您尝试了frame_2().grid.forget() - 它有什么问题? You would need to call something on a function call ( frame_2() ) instead of an object ( frame2 ).你会需要调用的东西在一个函数调用( frame_2()而不是一个对象( frame2 )。 This object was also not declared as global so no chance to get a hold of the object you wanted but declaring it globally.这个对象也没有被声明为全局对象,所以没有机会获得你想要的对象,而是全局声明它。 (Including initialization and usage) What else is wrong about that line? (包括初始化和使用)那一行还有什么问题? The function is called grid_forget and not forget on the grid object that is part of your "function call object"该函数称为grid_forget并且不要forget作为“函数调用对象”一部分的网格对象
  • Second thing: you used the same function name for two different functions.第二件事:你对两个不同的函数使用了相同的函数名。 Do not do that .不要那样做 Just don't.只是不要。

 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