Tkinter .pack() window not showing?

Question:

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()
Asked By: epcisky21

||

Answers:

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.

Answered By: user2555451

It seems you are using Python3, as there are parentheses after print, so from Tkinter import * should be from tkinter import *. Python is case-sensitive. You also forgot to call root.mainloop() at the end of your code as @user2555451 mentioned, although a window should appear all the same, but stop responding when any event occurs (e.g., clicks, key presses, focus changes) or briefly appear before closing itself.

Answered By: Xbox One