Python 3.x 更新或重置tkinter显示

Python 3.x 更新或重置tkinter显示,python-3.x,user-interface,tkinter,window,Python 3.x,User Interface,Tkinter,Window,我正在开发一个小Tkinter程序,一旦启动,它会提示您输入一个名称,然后单击“提交”,它将显示“欢迎来到我的世界”。我在检索输入并将其显示在新窗口或使用新信息更新窗口时遇到问题,但它将Py_Var1显示为条目名称。我做错了什么,是因为我试图在新窗口中显示信息,还是我使用了错误的功能 这是我的密码 from tkinter import * root = Tk() #Functions def info(): a= entry_1.get() def close_window(

我正在开发一个小Tkinter程序,一旦启动,它会提示您输入一个名称,然后单击“提交”,它将显示“欢迎来到我的世界”。我在检索输入并将其显示在新窗口或使用新信息更新窗口时遇到问题,但它将Py_Var1显示为条目名称。我做错了什么,是因为我试图在新窗口中显示信息,还是我使用了错误的功能

这是我的密码

from tkinter import *

root = Tk()

#Functions

def info():
    a= entry_1.get()


def close_window(root):
        root.destroy()
def comb(event=None):
       info()
       close_window(root)



#Display 
input_1 = Label(root, text=" Name: ", bg= "light grey", fg="blue", font=("Arial", 16))
entry_1 = Entry(root, bg= "white", fg= "black", bd= 5, relief= SUNKEN, font=("Arial", 12))
button = Button(root, text="Submit", command=comb, bd= 6, relief= RAISED, fg='blue', font=("Arial", 12))
root.bind("<Return>", comb)

aVar = StringVar(entry_1.get())
aVar.set(aVar)
#entry display
input_1.grid(row=1, sticky=E)
entry_1.grid(row=1, column=1)
button.grid(row=3, column=1)

root.mainloop()
##Second Window
root = Tk()

Var = StringVar()
Var.set(info)

t1 = Label(root, text="Welcome")
t2 = Label(root, text= Var)
t3 = Label(root, text="to my world")

#Display

t1.grid(row=1, column=1)
t2.grid(row=1, column=2)
t3.grid(row=1, column=3)

root.mainloop()
从tkinter导入*
root=Tk()
#功能
def info():
a=条目_1.get()
def关闭_窗口(根):
root.destroy()
def梳(事件=无):
信息()
关闭窗口(根目录)
#展示
输入1=标签(根,text=“Name:”,bg=“浅灰色”,fg=“蓝色”,font=(“Arial”,16))
条目1=条目(根,bg=“白色”,fg=“黑色”,bd=5,浮雕=凹陷,字体=(“Arial”,12))
按钮=按钮(root,text=“Submit”,command=comb,bd=6,relief=RAISED,fg='blue',font=(“Arial”,12))
root.bind(“,comb)
aVar=StringVar(条目_1.get())
aVar.set(aVar)
#入口显示
输入_1.网格(行=1,粘滞=E)
条目1.网格(行=1,列=1)
button.grid(行=3,列=1)
root.mainloop()
##第二窗口
root=Tk()
Var=StringVar()
变量集(信息)
t1=标签(root,text=“欢迎”)
t2=标签(根,文本=变量)
t3=标签(根,text=“到我的世界”)
#展示
t1.网格(行=1,列=1)
t2.网格(行=1,列=2)
t3.网格(行=1,列=3)
root.mainloop()

我认为问题在于您试图访问一个变量,该变量是您在销毁窗口之前分配的,而Tkinter无法访问该变量。需要一个全局变量。您的代码现在应该可以工作了

from tkinter import *

root = Tk()

#Functions

def info():
    global a
    a= entry_1.get()


def close_window(root):
        root.destroy()
def comb(event=None):
       info()
       close_window(root)



#Display 
input_1 = Label(root, text=" Name: ", bg= "light grey", fg="blue", font=("Arial", 16))
entry_1 = Entry(root, bg= "white", fg= "black", bd= 5, relief= SUNKEN, font=("Arial", 12))
button = Button(root, text="Submit", command=comb, bd= 6, relief= RAISED, fg='blue', font=("Arial", 12))
root.bind("<Return>", comb)


#entry display
input_1.grid(row=1, sticky=E)
entry_1.grid(row=1, column=1)
button.grid(row=3, column=1)

root.mainloop()
##Second Window
root = Tk()

t1 = Label(root, text="Welcome "+str(a)+" to my world")
##t2 = Label(root, text= Var)
##t3 = Label(root, text="to my world") # cleaner this way

#Display

t1.grid(row=1, column=1)
#t2.grid(row=1, column=2)
#t3.grid(row=1, column=3)

root.mainloop()
从tkinter导入*
root=Tk()
#功能
def info():
全球a
a=条目_1.get()
def关闭_窗口(根):
root.destroy()
def梳(事件=无):
信息()
关闭窗口(根目录)
#展示
输入1=标签(根,text=“Name:”,bg=“浅灰色”,fg=“蓝色”,font=(“Arial”,16))
条目1=条目(根,bg=“白色”,fg=“黑色”,bd=5,浮雕=凹陷,字体=(“Arial”,12))
按钮=按钮(root,text=“Submit”,command=comb,bd=6,relief=RAISED,fg='blue',font=(“Arial”,12))
root.bind(“,comb)
#入口显示
输入_1.网格(行=1,粘滞=E)
条目1.网格(行=1,列=1)
button.grid(行=3,列=1)
root.mainloop()
##第二窗口
root=Tk()
t1=标签(root,text=“欢迎”+str(a)+“来到我的世界”)
##t2=标签(根,文本=变量)
##t3=标签(root,text=“到我的世界”)#这样更干净
#展示
t1.网格(行=1,列=1)
#t2.网格(行=1,列=2)
#t3.网格(行=1,列=3)
root.mainloop()

它没有运行,因为有许多错误并且没有逻辑。 您使用了许多没有原因的函数,它们都不返回值。 此外,关闭根窗口并在此之后销毁条目小部件 您要求使用一个不返回任何内容的函数从刚刚销毁的条目中获取文本。即使您不破坏根窗口并使用顶级窗口,该程序仍然无法工作,因为您的函数不返回任何内容


看起来您不了解函数的基本用法。在尝试更复杂的事情之前,先考虑用简单的程序来播放函数。