2016-08-24 38 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

欲了解更多信息,请查看[this](http://stackoverflow.com/a/8834940/5601431)[@BryanOakley]的答案(http://stackoverflow.com/users/7432/bryan-oakley)。 –