2017-08-14 156 views
1

我的程序一次生成一个图形,每个图形都有一个退出按钮。 程序暂停在mainloop,直到我按下按钮,然后生成下一个图。如何以编程方式通过tkinter画布退出主循环按钮

我想一种以编程方式按或调用关联到该按钮的动作,在这种情况下root.quit()

我已经打过电话上的按钮invoke()但这不起作用。我的感觉是这个事件在mainloop开始之前就已经丢失了。

from tkinter import * 

pause = False # passed in as an arg 

root = Tk() 
root.title(name) 

canvas = Canvas(root, width=canvas_width, height=canvas_height, bg = 'white') 
canvas.pack() 

quit = Button(root, text='Quit', command=root.quit) 
quit.pack() 

# make sure everything is drawn 
canvas.update()   

if not pause: 
    # Invoke the button event so we can draw the next graph or exit 
    quit.invoke() 

root.mainloop() 

回答

1

我意识到的问题是与事件丢失和mainloop阻断所以就用pause ARG来确定何时在最后图表运行mainloop,即。

Tkinter understanding mainloop

显示所有图表,当你按下任何窗口退出所有窗口消失,程序结束。

如果有更好的方法来做到这一点,请让我知道,但这个工程。

root = Tk() 
root.title(name) # name passed in as an arg 

# Creation of the canvas and elements moved into another function 
draw(root, ...) 

if not pause: 
    root.update_idletasks() 
    root.update() 
else: 
    mainloop() 
相关问题