【问题标题】:I cant destroy buttons tkinter我不能破坏按钮 tkinter
【发布时间】:2020-08-05 14:19:18
【问题描述】:

我有这个代码:

from tkinter import *

root = Tk()
root = root
root.title("1")
root.geometry('600x650')

def click():
   b.destroy()
b = (1, 2, 3, 4, 5)
for i in b:
   b = Button(root, text=i, command=click)
   b.pack()

print("success")

root.mainloop()

我需要销毁按钮,我点击了它,但它销毁了最后生成的按钮。

请不要推荐这样的代码:

from tkinter import *

root = Tk()
root = root
root.title("1")
root.geometry('600x650')

def click1():
    button1.destroy()
def click2():
    button2.destroy()

button1 = Button(root, text="1", command=click1)
button2 = Button(root, text="2", command=click2)
button1.pack()
button2.pack()

print("success")

root.mainloop()

我需要每个按钮都能自行关闭。

【问题讨论】:

  • 您只是一遍又一遍地创建一个按钮。您没有像预期的那样创建按钮列表。

标签: python-3.x tkinter


【解决方案1】:

这是我在评论中的意思的一个例子。

from tkinter import *
from functools import partial

root = Tk()
root = root
root.title("1")
root.geometry('600x650')

# specify which button to click
def click(index):
    buttons[index].destroy()

b = (1, 2, 3, 4, 5)
# keeping our buttons in a list allows us to destroy them individually
buttons = []
for index, i in enumerate(b):
    # use functools.partial to hand arguments to the callback function
    # the argument being the button you are clicking  
    buttons.append(Button(root, text=i, command=partial(click, index)))
    buttons[index].pack()

print("success")

root.mainloop()

这是您提供的示例以及失败的地方。

from tkinter import *

root = Tk()
root = root
root.title("1")
root.geometry('600x650')

# the function destroys b
# that b is the last button you created in your loop below
def click():
   b.destroy()

# you create a tuple of numbers and assign it to b
b = (1, 2, 3, 4, 5)
for i in b:
   # for every loop a button is created and packed and assigned to b
   # but that is overwritten on every subsequent loop 
   # since you keep assigning to b
   b = Button(root, text=i, command=click)
   b.pack()

print("success")

root.mainloop()

【讨论】:

    【解决方案2】:

    在本节中,您将元组定义为 b,然后在每次迭代中覆盖 b:

    b = (1, 2, 3, 4, 5)
    for i in b:
       b = Button(root, text=i, command=click)
       b.pack()
    

    但主要问题是b 中只有 1 个按钮,您想要的是将每个按钮存储在 list 中,例如:

    from tkinter import *
    
    root = Tk()
    root = root
    root.title("1")
    root.geometry('600x650')
    
    def click(idx):
        # Here x is used to determine the button's location in list b.
        b[idx].destroy()
    
    buttons = (1, 2, 3, 4, 5)
    b = [] # Make b a list.
    for x, i in enumerate(buttons):
        b.append(Button(root, text=i,
                        # Here lambda stores x for each iteration, and assigns it to click.
                        command= lambda idx = x: click(idx)))
        b[-1].pack() # Pack the last button appended to b.
    
    print("success")
    
    root.mainloop()
    

    基本上是一样的,使用 lambda,但我已经输入了它,所以我发布它。

    【讨论】:

      【解决方案3】:

      你可以试试这个

      from tkinter import *
      
      root = Tk()
      root = root
      root.title("1")
      root.geometry('600x650')
      bts = [1, 2, 3, 4, 5]
      
      f = Frame(root)
      f.pack()
      
      def click(val):
          global bts, root, b, f
      
          f.destroy()
          f = Frame(root)
          f.pack()
      
          bts.remove(val)
          for i in bts:
              b = Button(f, text=i, command=lambda x=i: click(x))
              b.grid(row=int(i - 1), column=1)
      
      
      for i in bts:
         b = Button(f, text=i, command=lambda x=i: click(x))
         b.grid(row=int(i-1), column=0)
      
      print("success")
      
      root.mainloop()
      

      【讨论】:

        猜你喜欢
        • 2019-06-22
        • 2021-12-31
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-30
        • 2012-05-16
        • 1970-01-01
        相关资源
        最近更新 更多