2016-08-24 2 views
0

a。少数のクリックの後にボタンを削除したいというシナリオがあります。 b。しかし、ボタンが最後のクリックに達すると、そのボタンは破壊されません。以下に示すよう コード:グリッドから削除しようとしているときにボタンウィジェットが破棄されない

from tkinter import * 

class test_button: 
    def __init__(self, master): 
     self.master = master 
     self.next_button = None 

     if not (self.next_button): 
      self.next_button = Button(root, background="orange red", activebackground="orangered3", text="Next Test Config", command=self.next_button_code).grid(row=1, column=1) 

    def next_button_code(self): 
     if self.next_button: 
      self.next_button.destroy(); self.next_button = None 

# Top Local Variables 
root = Tk() 

# Top Level Default Codes 
my_gui = test_button(root) 

root.mainloop() 

は、私は何も足りませんか?親切なあなたのコメントをドロップ!

+0

あなたはこれをデバッグするために何をしましたか? 'self.next_button'があなたの考えであることを確認しましたか? (ヒント:そうではありません) –

+0

インデントレベルのいくつかが間違っています。 –

答えて

1

変更

self.next_button = Button(root, background="orange red", activebackground="orangered3", text="Next Test Config", command=self.next_button_code).grid(row=1, column=1) 

へ:

self.next_button = Button(root, background="orange red", activebackground="orangered3", text="Next Test Config", command=self.next_button_code) 
self.next_button.grid(row=1, column=1) 
+0

説明とコメントをありがとう! – Vimo

+0

詳細については、[こちら](http://stackoverflow.com/a/8834940/5601431)[@BryanOakley](http://stackoverflow.com/users/7432/bryan-oakley)の回答を参照してください。 –

関連する問題