epcisky21 epcisky21 - 2 months ago 23
Python Question

Tkinter .pack() window not showing?

I've been working on a simple program that make a button output something. But when i run it,
thisenter image description here

(I got this from the internet btw) does not show up. Is somethoing wrong with the code or something?
Please help me so the window above can appear :)

Code:

from Tkinter import *
def asdf():
print('test')
tk = Tk()
b = Button(tk, text="test", command=asdf)
b.pack()

Answer

You forgot to call the Tk.mainloop method at the end of your program:

from Tkinter import *
def asdf():
    print('test')
tk = Tk()
b = Button(tk, text="test", command=asdf)
b.pack()
##############
tk.mainloop()
##############

Doing so starts Tkinter's main event loop and creates the window.

Comments