epcisky21
epcisky21

Reputation: 53

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()

Upvotes: 0

Views: 4057

Answers (2)

Xbox One
Xbox One

Reputation: 303

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.

Upvotes: 1

user2555451
user2555451

Reputation:

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.

Upvotes: 5

Related Questions