私は2つのウィンドウを表示しています。 Entryウィジェットで私のゲームを保存するための最初のもの。そして2番目は私のゲームです。.destroy()はアプリケーションを終了しません。
私はウィジェットを.destroy()
で閉じると機能します。しかし、その後、私はfenetre.destroy()
をしてゲームを去りたいが、何も起こらない。そして、私は手動でウィンドウを閉じたときに、私はメッセージエラーを得た:
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed
これは私のコードです:
def game_quit():
global name
if askyesno("Quit game ?","Are you sure? :("):
if askyesno("Save ?","Do you want to save your game? "):
ask_name()
save_scoreG(grid,lenght)
fenetre.destroy()
def ask_name():
global entry, master
master = Toplevel()
master.title("Your Name")
button=Button(master, text='Input your name and click here', command = get_name, bg= "yellow")
usertext= StringVar()
entry = Entry(master, textvariable=usertext)
entry.pack()
button.pack()
master.mainloop()
def get_name():
global name, entry, master
name = str(entry.get())
master.destroy()
def save_scoreG(grid,lenght):
global name
with open('score','a') as s:
s.write(str(lenght)+':' + name +':'+ str(score(grid,lenght))+'\n')
私はシェルでエラーを取得するには、これを簡略化することができません。
>>> from tkinter import Toplevel, Tk
>>> fenetre = Tk()
>>> w = Toplevel()
>>> w.destroy()
>>> fenetre.destroy()
>>> fenetre.destroy()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "Z:\py34-64\lib\tkinter\__init__.py", line 1842, in destroy
self.tk.call('destroy', self._w)
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed
コールバックを割り当てて関数を返してみましたか? – Angrywasabi
問題を実行して複製する実際のコードを教えてください。ウィンドウを2回破棄しようとするとエラーが出るだけなので、結論はまさにあなたがやっていることです。 2番目の例と同じコマンドを実行すると、私はあなたが言っているエラーを取得しません。 –
'mainloop'を複数回呼びますか?それは 'ask_name'でそれを呼び出すことに疑いがあります。 –