简体   繁体   中英

Tkinter .pack() window not showing?

I've been working on a simple program that make a button output something. But when i run it, this在此处输入图片说明

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

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.

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 (eg, clicks, key presses, focus changes).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM