Anaconda Spyder下的Tkinter中的销毁问题

时间:2018-12-02 10:27:17

标签: anaconda ipython spyder

在以下程序中,我可以使用其关闭按钮关闭窗口,但不能使用“ 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)

有任何提示吗?谢谢。

1 个答案:

答案 0 :(得分:0)

(此处为 Spyder维护程序),您可以通过两种选择使代码在Spyder中运行:

  1. 转到

    import React from "react"; import ReactDOM from "react-dom"; const styleApp = { textAlign: 'center', width: '100vw', height: '100vh' } class App extends React.Component { constructor() { super(); this.state = { position: [], intervalId: null }; } componentDidMount() { const intervalId = setInterval(() => { this.move() this.renderList() //this.addDiv(event) //start moving almoust all div's, without first one. In the console this.state.position show: NaN }, 1000); this.setState({ intervalId }); } componentWillUnmount() { clearInterval(this.state.intervalId); } addDiv = event => { const newStateArray = [...this.state.position]; newStateArray.push([event.pageX, event.pageY]); this.setState({ x: event.pageX, y: event.pageY, position: newStateArray }); this.setState((state) => { console.log(`positions for all clicks: ${state.position}`); }); }; move = () => { this.setState((state, props) => { state.position.forEach((item, index) => { let i = item; i[0] += 20; i[1] += 1; console.log(i); }) }); }; renderList = () => { return ( <div> {this.state.position.map((item, index) => ( <div key={index} style={{ position: "absolute", width: "30px", height: "30px", background: "blue", top: this.state.position[index][1], left: this.state.position[index][0] }} /> ))} </div> ) } render() { return ( <div onClick={this.addDiv} className="App" style={styleApp}> <p>This is x: {this.state.x}</p> <p>This is y: {this.state.y}</p> <div> { this.renderList() } </div> </div> ); } } const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement);

    并停用名为Tools > Preferences > IPython console > Graphics的选项。然后,在运行代码之后,它将阻塞控制台,但是您不会遇到任何其他问题。

  2. 如果您选择Activate support作为后端,

    Tk

    然后您需要从代码中删除Tools > Preferences > IPython console > Graphics,因为使用我们的首选项会创建一个Tk事件循环,这样您的代码就不会阻塞控制台,从而使app.root.mainloop()变得不必要。

    < / li>