Okay so I'm trying to learn some GUI using tkinter following along this website's guide: https://realpython.com/python-gui-tkinter/#building-your-first-python-gui-application-with-tkinter Depending on how I run the code it works. So if I run it in shell(line by line) it works and will output whatever "Name" is manually set to when you call "name". My question is how do I get it to run in idle(editor).
What I believe happens in Idle is it is calling name long before anything is ever actually typed in the entry. which is why the output is "" . Where in shell I can type something into the entry "john doe" and when I get to 'name' it has something it can output.
Some questions I'm coming to at this point are. . . Is this working like this is supposed to work in IDLE and Shell or am I missing something? Was this section of the tutorial only intended to run in shell? How would I run this to work in IDLE so when I get to a point where I have a fully functioning window it does accept a users input and allow it to be manipulated?
import tkinter as tk
window = tk.Tk()
label = tk.Label(text = "Name")
entry = tk.Entry()
label.pack()
entry.pack()
name = entry.get()
name
CodePudding user response:
Typically you would create a button that initiates the .get
and start the mainloop for the window, like this:
import tkinter as tk
def print_entry(widget):
name = widget.get()
print(name)
window = tk.Tk()
label = tk.Label(text = "Name")
entry = tk.Entry()
button = tk.Button(text = "Get Entry",
command = lambda e = entry: print_entry(e))
label.pack()
entry.pack()
button.pack()
window.mainloop()