2017-08-14 25 views
1

私のプログラムは、一度に1つずつ複数のグラフを生成し、それぞれに終了ボタンがあります。 ボタンを押すまで、プログラムはmainloopで一時停止し、次のグラフを生成します。tkinter canvas経由で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() 

答えて

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() 
関連する問題