Reputation: 1
from tkinter import *
def create_main_window():
global main_window
main_window = Toplevel()
main_window.update()
entrance_window = Tk()
first_text_label = Label(entrance_window, text="you are in:").grid(row=0, column=0)
place_entry = Entry(entrance_window).grid(row=0, column=1)
submit_button = Button(entrance_window, text="Submit", command=create_main_window).grid(row=1, column=0, columnspan=2)
Label(main_window, text=f"{place_entry}").pack()
entrance_window.mainloop()
the program should open a new window with the text from the entry box from the first window but it either shows None
if I write
Label(main_window, text=f"{place_entry}").pack()
in the create_main_window or it gives me an error saying that main_window is not defined if I write it after the button code.
Can someone help with this?
Upvotes: 0
Views: 263
Reputation: 4537
Never used global
. Don't use the wildcard from tkinter import *
The Label
widget must be placed inside the create_main_window() function
when you want to see the widget.
snippet:
import tkinter as tk
def create_main_window() -> None :
main_window = tk.Toplevel()
tk.Label(main_window, text=f"{place_entry.get()}").pack()
entrance_window = tk.Tk()
first_text_label = tk.Label(entrance_window, text="you are in:").grid(row=0, column=0)
place_entry = tk.Entry(entrance_window)
place_entry.grid(row=0, column=1)
tk.Button(entrance_window, text="Submit", command=create_main_window).grid(row=1, column=0, columnspan=2)
entrance_window.mainloop()
if __name__=='__main__':
create_main_window()
Screenshot:
Upvotes: 0
Reputation: 7680
Try this:
from tkinter import *
def create_main_window():
global main_window
main_window = Toplevel(main_window)
label = Label(main_window, text=f"{place_entry.get()}")
label.pack()
# main_window.update() # This is useless
entrance_window = Tk()
first_text_label = Label(entrance_window, text="You are in:")
first_text_label.grid(row=0, column=0)
place_entry = Entry(entrance_window)
place_entry.grid(row=0, column=1)
submit_button = Button(entrance_window, text="Submit", command=create_main_window)
submit_button.grid(row=1, column=0, columnspan=2)
entrance_window.mainloop()
I moved the label creation inside create_main_window
. Also please note that using var = a().b()
, saves what ever b()
returns inside var
. That is why when you use var = Entry(...).pack(...)
, var
is always None
.
Upvotes: 1
Reputation: 866
This is because you are trying to add a Label to an object that doesn't exist. Move the Label function to the create_main_window() function, like below:
from tkinter import *
def create_main_window():
global main_window, entrance_window
main_window = Toplevel()
place_entry = Entry(entrance_window).grid(row=0, column=1)
Label(main_window, text=f"{place_entry}").pack()
main_window.update()
entrance_window = Tk()
first_text_label = Label(entrance_window, text="you are in:").grid(row=0, column=0)
submit_button = Button(entrance_window, text="Submit", command=create_main_window).grid(row=1, column=0, columnspan=2)
entrance_window.mainloop()
Upvotes: 0