【问题标题】:How to programmatically quit mainloop via a tkinter canvas Button如何通过 tkinter 画布按钮以编程方式退出主循环
【发布时间】:2017-08-14 14:51:12
【问题描述】:

我的程序一次生成多个图表,每个图表都有一个退出按钮。 程序在mainloop 暂停,直到我按下按钮,然后生成下一个图表。

我想要一种以编程方式按下或调用与该按钮关联的操作的方法,在本例中为 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()

【问题讨论】:

    标签: python-3.x canvas tkinter tkinter-canvas


    【解决方案1】:

    我意识到问题在于事件丢失和mainloop 阻塞,因此我使用pause 参数来确定何时运行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()
    

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 2020-09-21
      • 2018-11-26
      • 1970-01-01
      • 2011-06-16
      • 2021-11-11
      • 2012-02-16
      • 2018-12-03
      • 1970-01-01
      相关资源
      最近更新 更多