gpt4 book ai didi

python - Emacs:劣质模式 python-shell 出现 "lagged"

转载 作者:太空狗 更新时间:2023-10-30 00:08:34 26 4
gpt4 key购买 nike

我是 Python(3.1.2)/emacs(23.2) 新手,使用发现的 pythonware 教程自学 tkinter here .相关代码贴在问题下方。

问题:当我点击 Hello 按钮(应该调用 say_hi 函数)时,为什么劣质 python shell(即我用 C-c C-c 启动的那个)等待执行 say_hi 打印函数,直到我 a) 单击退出按钮或 b) 关闭根小部件?当我在 IDLE 中尝试相同的操作时,每次单击 Hello 按钮都会在 IDLE python shell 中立即打印,甚至在我单击 Quit 或关闭根小部件之前。

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

最佳答案

我猜想,由于没有附加到 tty,Python 解释器(通过 C stdio)从行缓冲切换到 block 缓冲,并且在关闭之前不会刷新标准输出。在“劣质 Python:运行 Shell 编译”缓冲区中运行 os.isatty(1) 返回 false,从而增加了这个猜测的权重。

def say_hi(self):
print("hi there, everyone!")
sys.stdout.flush()

可能会有所作为。

关于python - Emacs:劣质模式 python-shell 出现 "lagged",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2861178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com