【发布时间】:2010-05-18 21:08:28
【问题描述】:
我是 Python(3.1.2)/emacs(23.2) 新手,使用找到的 pythonware 教程自学 tkinter here。相关代码粘贴在问题下方。
问题:当我单击 Hello 按钮(它应该调用 say_hi 函数)时,为什么劣质 python shell(即我用 Cc Cc 启动的那个)等待执行 say_hi 打印函数,直到我要么 a) 单击退出按钮或 b) 关闭根小部件?当我在 IDLE 中尝试相同的操作时,每次单击 Hello 按钮都会在 IDLE python shell 中立即打印,甚至在我单击退出或关闭根小部件之前。
emacs 运行 Python shell(与 IDLE 相比)的方式是否存在一些怪癖,会导致这种“滞后”行为?在解决 Project Euler 问题时,我注意到 emacs 与 IDLE 有类似的滞后,但这是我见过的最清晰的示例。
仅供参考:我使用 python.el 并且有一个相对干净的 init.el...
(setq python-python-command "d:/bin/python31/python")
是我的 init.el 中唯一的一行。
谢谢,
迈克
=== 开始代码===
from tkinter import *
class App:
def __init__(self,master):
frame = Frame(master)
frame.pack()
self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print("hi there, everyone!")
root = Tk()
app = App(root)
root.mainloop()
【问题讨论】:
-
试试
print('abc', file=sys.stderr)可能是缓冲问题(输出到控制台可能是行缓冲,但输出到管道/文件可能有固定缓冲区)。 -
没用。 sys.stdout.flush() [msw's answer below] 工作。不过,感谢您的评论!