Python Forum

Full Version: Unable to create checkbox and select at run time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello All,

I am facing some challenge with my user interface:

def PopUp():
        pop = Tk()
        count =output1.split()

        for  i in range(0,len(count)):
                var=[]

                l=Checkbutton(pop,text=output1.split()[i],variable=var.append(i)).grid(row=i+1,sticky=W)
This will print output as:
Output:
x ABC x CDE x DEF
Depending upon how many values are there in count

Now when user selects ABC and DEF that is checkbox 1 and 3,wanted to take these values and perform certain operations or all three checkbox.
so wanted to understand how can store the values that user selected and then utilize them further.
Why not use a tutorial such as this one?
You could store the data in a dictionary
import tkinter as tk


class MainFrame(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.checkbuttons = {'ABC': tk.IntVar(),
                             'CDE': tk.IntVar(),
                             'EFG': tk.IntVar()}
        
        for key, value in self.checkbuttons.items():
            ctrl = tk.Checkbutton(self, text=key, variable=value)
            ctrl.pack()
        
        self.button = tk.Button(
            self, text='Checkbutton values', command=self.on_button)
        self.button.pack(pady=15)
        self.pack()

    def on_button(self):
        print('checkbutton values')
        for key, value in self.checkbuttons.items():
            print(f'{key}: {value.get()}')


if __name__ == '__main__':
    app = tk.Tk()
    main_frame = MainFrame()
    app.mainloop()
Hello Yoriz,

I am not clear here,this list gets created at run time and then I want to translate this list into set of checkboxes,which user can interact with and then process the user input further.

Thanks and Regards
Tej Gandhi
Give an a few examples of what the list could contain and in what way you plan to react on the result of the checkbutton choices.
Hello Yoriz,all,

It creates a list of running processes or something similar to that,the we determine which processes the user has selected and provide more description and details on it.

Thanks and Regards
Tej Gandhi