簡體   English   中英

如果選中復選框,tkinter 如何刪除復選框和標簽

[英]tkinter how delete both checkbox and label if checked box

我試圖選中一個復選框並使用上方的按鈕刪除“選中的框”和“標簽”。 它們都是從同一個列表在循環中創建的。 我試圖構建一個以“buttons”為鍵和“result_of_checkboxes”為值的字典,如果值與“”不同,則銷毀鍵。 這是行不通的。 如果按鈕是鑰匙,為什么我不能銷毀它們? 正確的做法是什么? 提前致謝!

from tkinter import *
    
root = Tk()    
root.geometry('400x400')
    
def destroy_button():
    for key, value in dict_check_and_buttons:
        if value != '' and current_var != '':
            key.destroy()      
            current_box.destroy()    

my_friends = ['Donald', 'Daisy', 'Uncle Scrooge']
    
button1= tk.Button(root, text = "delete both if cbox is checked", command = destroy_button).pack()
    
#-----ild checkbuttons and store results in checkbutton_result list

checkbutton_result = []

for index, friend in enumerate(my_friends):
    current_var = tk.StringVar()
    current_box = tk.Checkbutton(root, text= friend,
                                 variable = current_var,
                                 onvalue = friend, offvalue = ""
                                 )
    checkbutton_result.append(current_var) #append on and off results
    current_box.pack()
        
#-----build buttons and store them in buttons_list
buttons_list = []

for index, friend in enumerate(my_friends):
    buttons= tk.Button(root, text = friend).pack()
      
    buttons_list.append(buttons)
        
#-----build a dict with list to say "if  onvalue != '' destroy button"    

dict_check_and_buttons = dict(zip(buttons_list, checkbutton_result))   

root.mainloop()

 

錯誤是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\python\python38\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "<ipython-input-18-954d3a090f2c>", line 7, in destroy_button
    for key, value in dict_check_and_buttons:
TypeError: cannot unpack non-iterable NoneType object

您的代碼中存在以下問題:

  • 由於以下行, buttons_list中的所有項目都為None
buttons= tk.Button(root, text = friend).pack()  # buttons is the result of pack()

該行應分為兩行:

buttons = tk.Button(root, text=friend)
buttons.pack()
  • 您無法通過迭代字典來獲得 (key, value) 對
for key, value in dict_check_and_buttons:  # dict_check_and_buttons is a dictionary
    ...

相反,您應該迭代dict_check_and_buttons.items()的結果:

for key, value in dict_check_and_buttons.items():
    ...
  • 你需要在tk.StringVar()上調用get() tk.StringVar()
for key, value in dict_check_and_buttons.items():
    if value.get() != '':  # use value.get()
        key.destroy()

如果您還需要銷毀復選按鈕,則需要將復選按鈕及其關聯變量保存到checkbutton_result中:

checkbutton_result = []

for index, friend in enumerate(my_friends):
    current_var = tk.StringVar()
    current_box = tk.Checkbutton(root, text= friend,
                                 variable = current_var,
                                 onvalue = friend, offvalue = ""
                                 )
    checkbutton_result.append((current_box, current_var)) # save both checkbutton and its associated variable
    current_box.pack()

然后銷毀destroy_button()

def destroy_button():
    for btn, (cb, cbvar) in dict_check_and_buttons.items():
        if cbvar.get() != '':
            btn.destroy()
            cb.destroy()

有了你的回答,我重建了代碼。 多虧了你,它現在很完美。

import tkinter as tk

root = tk.Tk()
root.geometry('400x400')
'''
to get if checkbox is checked to use results to delete checkbox and label
'''


def destroy_button_box():
    for button_names, (ckbox, on_off) in dict_buttons_box_var.items():
        if on_off.get() != 0:
            button_names.destroy()
            ckbox.destroy()
        


my_friends = ['Donald', 'Daisy', 'Uncle Scrooge']

button1 = tk.Button(root, text="delete label if box is checked", command=destroy_button_box).pack()


checkbox_variable_list = []

box_list = []

for index, friend in enumerate(my_friends):
    #     current_var = tk.IntVar()
    current_var = tk.IntVar()
    #   current_var = tk.StringVar()
    current_box = tk.Checkbutton(root, text=friend,
                                 variable=current_var,
                                 onvalue=1, offvalue=0
                                 )

    current_box.pack()
    
    # append checkbox and 'on' or 'off' results
    checkbox_variable_list.append((current_box, current_var))

# -----build buttons and store them in buttons_list
buttons_list = []
for index, friend in enumerate(my_friends):
    button_names = tk.Button(root, text=friend)
    button_names.pack()
    
    # append buttons in loop
    buttons_list.append(button_names)

# -----build a dict with lists to use in destroy_box_button function:
dict_buttons_box_var = dict(zip(buttons_list, checkbox_variable_list))

root.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM