2021年5月2日星期日

How to make radio button active based on check box is selected or not in python

I have a list of CheckButtons(Apples, Rose, Red) created and each CheckButton carries two options(default, custom) which are represented with RadioButtons as below. 1). I want radio button to be active only based on respective checkbox selection, should be disabled if check box is not checked and at the end, 2). based on check boxes and radio button choices the entries should be appended to two lists(list_d, list_c). I am able to print list with selected checkboxes and radio button choices. But two lists are being appended based on last radio button selection. All of them either added to list_b or list_c based on last radio button selection.

import tkinter   from tkinter import *  root = Tk()  my_list = {'apple ' : 0, 'rose' : 0, 'red' : 0}      list = []  var_list = []  var = []  de = 'default'  cu = 'custom'  for item in my_list:      my_list[item] = Variable()      radio = Variable()      c = Checkbutton(root, text=item, variable=my_list[item], onvalue=item, offvalue="").pack(anchor=W)            r = Radiobutton(root, text=de, variable=radio, value=de).pack(anchor=E)      r2 = Radiobutton(root, text=cu, variable=radio, value=cu).pack(anchor=E)        var_list.append(my_list[item])      var.append(radio)    def add_to_list():      global list      list = []      list_d = []      list_c = []        for i in var_list:          if i.get() !="":              list.append(i.get())      for v in var:          if radio.get() == de:              list_d.append(v.get())          elif radio.get() == cu:              list_c.append(v.get())        print(list)      print(list_d)      print(list_c)    Button(root, text='OK', command=add_to_list).pack(anchor=W)  root.mainloop()  

Thank you community for the help

https://stackoverflow.com/questions/67333587/how-to-make-radio-button-active-based-on-check-box-is-selected-or-not-in-python April 30, 2021 at 08:05PM

没有评论:

发表评论