简体   繁体   English

tkinter python脚本无法在Windows中运行

[英]tkinter python script does not work windows

I am studying Python. 我正在学习Python。 Doing exercises from a book. 从书上做练习。 I wrote a simple example script using Tkinter module. 我使用Tkinter模块编写了一个简单的示例脚本。 I use Windows XP Pro. 我使用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. 当我从命令行或在Idle内部运行此脚本时,脚本会在一秒钟内退出,并且不会出现任何窗口。

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? 好的,伙计们,我终于发现了这个错误……为什么Tkinter无法正常工作。该脚本在屏幕上创建了一个窗口框架,但是一旦脚本结束,该框架便消失了。通过在最后一行对raw_input的调用来解决这个问题。.但是按并不会使脚本退出...必须使用Control-C很奇怪。.也许这在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.. 我为Tkinter下载了一个名为Graphics.py的包装器。它有一个显式的win.close方法-直到调用它为止,框架仍然存在。

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: 以下代码在Win 8.1 Python 3.6上的PyCharm 2017.2.4下完美运行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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