PhilipBert
PhilipBert

Reputation: 35

Tkinter Toplevel window not appearing

Python 3.8, Win 10 is the os, Toplevel widget does not appear to be working with new window not appearing. Any guidance would be appreciated, thanks!

from tkinter import *

root = Tk()


def popup():
   top = Toplevel(root)
   my_label_top = Label(top, text="This is a Tkinter Popup")
   top.mainloop()


my_button = Button(root, text="Popup, click here", command="popup")
my_button.grid(row=0, column=0)

root.mainloop()

Upvotes: 0

Views: 182

Answers (1)

schwartz721
schwartz721

Reputation: 1027

Problem:

  • The only issue here is that the callback command shouldn't be a string.

Solution:

  • Remove the quotes around popup and the Toplevel window should appear.

Fixed Code:

from tkinter import *

root = Tk()


def popup():
    top = Toplevel(root)
    my_label_top = Label(top, text="This is a Tkinter Popup")
    my_label_top.pack()


my_button = Button(root, text="Popup, click here", command=popup)
my_button.grid(row=0, column=0)

root.mainloop()

Tips:

  • Using top.mainloop() is not necessary.
  • You also forgot to pack() the Label(my_label_top)

Upvotes: 2

Related Questions