I am using python 3.8.5 and tkinter 8.6. I am learning to use tkinter. I was trying to make a simple form but for some reason I get two windows when I run my code. One of these windows has everything I am expecting with all the details I expect, but then there is a second unexepected uncallwed window with nothing in it. I cannot tell from my code why there is a second window as I only have one line where I call window.mainloop()
as well as I only create 1 window variable.
import tkinter as tk WIDTH=50 HEIGHT=1 BG="white" FG="black" COMPONENTS_MAP = ( {"label":"Name","type":"text"}, {"label":"Description","type":"text_area"}, {"type":"submit"} ) COMPONENTS = [] for mapping in COMPONENTS_MAP: if mapping["type"] == "text_area": COMPONENTS.append( tk.Label( text=mapping["label"], foreground=FG, background=BG, width=WIDTH ) ) COMPONENTS.append( tk.Text( fg=FG, bg=BG, width=WIDTH, height=3 ) ) if mapping["type"] == "text": COMPONENTS.append( tk.Label( text=mapping["label"], foreground=FG, background=BG, width=WIDTH ) ) COMPONENTS.append( tk.Entry( fg=FG, bg=BG, width=WIDTH ) ) if mapping["type"] == "submit": COMPONENTS.append( tk.Button( text="Submit", width=WIDTH, height=HEIGHT, bg=BG, fg=FG ) ) window = tk.Tk() for component in COMPONENTS: component.pack() window.mainloop()
Looks like this
Advertisement
Answer
You initialize window
after initializing of widgets. Move window = tk.Tk()
to begin of your code (right after importing for example).