简体   繁体   中英

tkinter python script does not work windows

I am studying Python. Doing exercises from a book. I wrote a simple example script using Tkinter module. I use Windows XP Pro.

This is it: ( literally from the book )

from Tkinter import *
root = Tk()
root.mainloop()

This is supposed to open a blank window on the desktop. When I run this, either from command line or inside Idle, the script exits within one second, and no window appears.

I tried importing other modules, and they all work fine. What am I missing?

Ok folks, I finally discovered the bug...why Tkinter would not work.. The script creates a window frame on screen, but as soon as the script ends, the frame disappears..not predicted by the book...I found this out by putting a call to raw_input as the last line..but then pressing would not get the script to exit...had to use Control-C weird..maybe it's a glitch in Windows?

I downloaded a wrapper for Tkinter called Graphics.py...this has an explicit win.close method - and until you call it, the frame remains..

So I guess I'll use that...

If anyone has some other workaround, I'd appreciate hearing it..

Thanks,

Following code works perfectly under PyCharm 2017.2.4 on Win 8.1 Python 3.6:

from tkinter import *

def hello(e):
    print("Hello")


tk = Tk()
# btn = Button(tk, text = "click me", width = 30, height = 5)
# btn.bind("<Button-1>", hello)
# btn.pack()
tk.mainloop()

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