0

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

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • 1
    Just a note that doesn't have to do with your question. please DONT use global variables, at all!! Using global variables is bad practice. https://stackoverflow.com/q/19158339/14644000 – Itay Dumay Apr 30 '21 at 12:09
  • @ItayDumay I will look into the link, thanks for the suggestion. I am new to coding, I know I am doing very stupid mistakes, I will keep it in mind in future. – Vishwanath DH Apr 30 '21 at 12:12
  • It's ok :) we're all learning – Itay Dumay Apr 30 '21 at 12:15
  • The problem is tkinter. It encourages this kind of non-object-oriented ad hoc programming. It has always been a hack, and the Python community has tried for years to get rid of it. tkinter doesn't actually run Python. Instead, Python has to generate command in an entirely different language (Tcl). As you get more sophisticated, throw tkinter in the trash and learn a real UI framework, like wxPython or Qt. – Tim Roberts May 03 '21 at 03:08
  • Are you saying you want these lists to continue to grow as more choices are made? If so, then you need to make `list_c` and `list_d` be globals as well, and not erase them at the start of `add_to_list`. – Tim Roberts May 03 '21 at 03:10
  • list_c and list_d when combined would make 'list'. Meaning I am splitting 'list' into list_c and list_d based on radio button selection(default or custom) – Vishwanath DH May 03 '21 at 03:27

0 Answers0