Emacs:辅助模式python-shell似乎“滞后”
我是一名刚接触Python(3.1.2)和emacs(23.2)的新手,正在自学tkinter,参考的是在这里找到的教程。相关的代码我会在问题下面贴出来。
问题是:当我点击“Hello”按钮(这个按钮应该会调用say_hi函数)时,为什么emacs里的Python命令行(也就是我用C-c C-c启动的那个)要等到我点击“Quit”按钮或者关闭主窗口后,才执行say_hi的打印功能?而在IDLE中,每次点击“Hello”按钮,IDLE的Python命令行都会立即显示结果,甚至在我点击“Quit”或关闭主窗口之前就会显示。
是emacs运行Python命令行的方式(和IDLE相比)有什么特别之处,导致这种“延迟”的表现吗?我在做Project Euler的问题时也注意到emacs和IDLE之间的类似延迟,但这是我见过的最明显的例子。
顺便说一下:我使用的是python.el,并且我的init.el相对干净……
(setq python-python-command "d:/bin/python31/python")
是我init.el中的唯一一行代码。
谢谢,
Mike
=== 开始代码===
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()
1 个回答
4
我猜因为没有连接到一个终端(tty),所以Python解释器(通过C语言的标准输入输出)会把输出方式从逐行缓冲切换到块缓冲,这样在关闭之前就不会刷新标准输出。运行 os.isatty(1)
在一个“Inferior Python:run Shell Compile”的环境中返回的是假值,这让我的猜测更有道理。
def say_hi(self):
print("hi there, everyone!")
sys.stdout.flush()
这可能会有影响。