【问题标题】:Problem with destroy in tkinter under Anaconda SpyderAnaconda Spyder 下 tkinter 的破坏问题
【发布时间】:2019-05-03 21:32:14
【问题描述】:

在下面的程序中,我可以使用关闭按钮关闭窗口,但既不能使用“Esc”绑定,也不能使用按钮回调。我被发送到 IPython 控制台,但窗口仍停留在此处,并带有一个旋转的轮子!在终端或 Anaconda IDLE 中运行良好。我使用的是 Mac、High Sierra、Anaconda 和 Spyder 的最新版本(IPython 7.1.1、Spyder 3.3.2)。我怀疑 Spyder 有问题。

from tkinter import *
class Myapp(object):
    def __init__(self):
        self.root = Tk()
        self.root.geometry('150x100+1+1')
        self.root.title('Root')
        self.root.bind('<Escape>', lambda e: self.root.destroy())
        self.button = Button(self.root, text='End Program', command=self.end)
        self.button.place(x=10, y=45)
        self.L = [1,2,3]   # result of an omitted computation
    def end(self):
        self.root.destroy()
app = Myapp()
app.root.mainloop()
print(app.L)

有什么提示吗?谢谢。

【问题讨论】:

    标签: anaconda ipython spyder


    【解决方案1】:

    这里是 Spyder 维护者)您有两个选项可以让您的代码在 Spyder 中运行:

    1. 前往

      Tools &gt; Preferences &gt; IPython console &gt; Graphics

      并停用名为Activate support 的选项。然后在运行你的代码后,它会阻塞控制台,但你不会有任何其他问题。

    2. 如果您在

      中选择Tk作为后端

      Tools &gt; Preferences &gt; IPython console &gt; Graphics

      那么您需要从您的代码中删除 app.root.mainloop(),因为使用我们的首选项会创建一个 Tk 事件循环,因此您的代码不会阻塞控制台,这使得 app.root.mainloop() 变得不必要。

    【讨论】:

    • 不,很抱歉,但在家里这些解决方案都不起作用。解决方案 1 工作了一次,但之后不再工作,即使在重新启动 Spyder 时也是如此。解决方案2不立即显示窗口并打印[1,2,3],而且我的代码应该“无处不在”运行,不是吗?
    • 我应该补充一点,这是一个在网络上找到的测试代码,它在 IDLE、PyCharm、Atom、终端中运行......它是为即将出版的书准备的......
    • 也许是我写的。有 Mac High-Sierra、Anaconda Python 和 Spyder 最新版本的人尝试过吗?
    猜你喜欢
    • 2016-02-01
    • 2020-03-16
    • 2021-03-15
    • 2017-12-09
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多