0

I'm working on a small Tkinter program where once started it prompts you to input a name then after clicking submit it will display "Welcome to my world". I'm having issues with either retrieving the input and displaying it in a new window or updating the window with the new information but it displays Py_Var1 as the entry name. What am I doing wrong, is it because I'm trying to display information in a new window or am I using the functions wrong?

Here is my code

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

2 Answers2

0

I think the problem was that you were trying to access a variable which you assigned before you destroyed the window after it was destroyed which Tkinter can't do. Needed a global variable. Your code should work now.

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()
douglas rouse
  • 148
  • 1
  • 2
  • 15
0

It is not running because there are many errors and no logic. You use many functions whithout reason and none of them returns values. Also, you destroy the Entry widget closing the root window and after that you are asking to get the text from the Entry you just destroyed using a function which don't return anything. Even if you don't destroy the root window and use a toplevel window, still this program will not work because your function don't return anything.

It looks like you don't understand the basic usage of functions. Consider to play with functions with simple programs before try something more complicated.

gms
  • 325
  • 3
  • 9