I am attempting to open a new window when the New Window button is pressed in the main "root" window. This currently works and does indeed open a second window. In the second window I want to ask the user for an input and then this input will be turned into a list of strings.
An example input would be "Amy, Bob, Carl"
. The expected output would be ['Amy', 'Bob', 'Carl']
but currently the program just returns ['']
.
My current code is:
from tkinter import *
root = Tk()
root.title("Welcome screen")
root.geometry("300x300")
def open_new_window():
top = Toplevel()
top.title("second window")
entities = Entry(top)
entries = entities.get().split(", ")
entities.pack()
entities.focus_set()
print(entries)
sub_button = Button(top, text="Submit", command= ?)
sub_button.pack(pady=20)
close_btn = Button(top, text="Close", command=top.destroy)
close_btn.pack()
open_button = Button(root, text="New Window", command=open_new_window)
open_button.pack(pady=20)
exit_button = Button(root, text="Close", command=root.destroy)
exit_button.pack(pady=20)
root.mainloop()
I am new to Tkinter and I am unsure why this is happening. I'm sure it's a simple silly mistake but I can't find where I am going wrong. I also am unsure as to whether I need a Submit button as I don't know what command should be passed to it.
Any advice is appreciated. Please let me know if any additional information is required.
CodePudding user response:
First, we will understand why you got a empty list: your code is executed sequentially, so when you do the entities.get()
you haven't yet written anything nor pressed "Submit", i.e., you want to read the entry box once you press the "Submit", not earlier, for that reason, you have the command = ?
.
As I am aware, you have mainly 2 options:
- Get the text from the button itself
- Create a variable linked to the entry box and read this
Method 1: read the data from the entry
from tkinter import *
root = Tk()
root.title("Welcome screen")
root.geometry("300x300")
def do_stuff(entry):
print(entry.get())
def open_new_window():
top = Toplevel()
top.title("second window")
entities = Entry(top)
entities.pack()
entities.focus_set()
sub_button = Button(top, text="Submit", command= lambda: do_stuff(entities))
sub_button.pack(pady=20)
close_btn = Button(top, text="Close", command=top.destroy)
close_btn.pack()
open_button = Button(root, text="New Window", command=open_new_window)
open_button.pack(pady=20)
exit_button = Button(root, text="Close", command=root.destroy)
exit_button.pack(pady=20)
root.mainloop()
Method 2: link a variable
from tkinter import *
root = Tk()
root.title("Welcome screen")
root.geometry("300x300")
def do_stuff(text_entry):
print(text_entry.get())
def open_new_window():
top = Toplevel()
top.title("second window")
text_entry = StringVar()
entities = Entry(top, textvariable = text_entry)
entities.pack()
entities.focus_set()
sub_button = Button(top, text="Submit", command= lambda: do_stuff(text_entry))
sub_button.pack(pady=20)
close_btn = Button(top, text="Close", command=top.destroy)
close_btn.pack()
open_button = Button(root, text="New Window", command=open_new_window)
open_button.pack(pady=20)
exit_button = Button(root, text="Close", command=root.destroy)
exit_button.pack(pady=20)
root.mainloop()
The main advantage in this last approach is that you can play with the text before and after the entry is built.