1

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.

flossy
  • 15
  • 5

1 Answers1

1

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:

  1. Get the text from the button itself
  2. 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.

DecowVR
  • 591
  • 4
  • 9
  • thank you, that worked perfectly! What does ```lambda``` do when used in ```command= lambda: do_stuff(entities)``` or ```command= lambda: do_stuff(text_entry)```? – flossy Mar 03 '22 at 18:42
  • 1
    `command` expects a function to call, for example in `open_button = Button(root, text="New Window", command=open_new_window)`, `open_new_window` is a function that will call when the event happens. The problem is that we want to call the function with an argument (`do_stuff(text_entry)` or `do_stuff(entities)`), however, if we use it like that: we have already called the function! `lambda` allows us to create a "anonymous function", it is like creating a function `def call_do_stuff(): do_stuff(text_entry)`, but without the need of creating the function itself – DecowVR Mar 03 '22 at 18:47