Jan-06-2023, 12:43 PM
I have a series of radio buttons on a grid, using custom tkinter.
I am setting each one up onto the grid using the following code:
As you can see, all I am doing is altering the position and value of each radio button. So, these lines are almost identical.
I know I should be able to loop over two lines and create the same concept but, for the life of me, I cannot see how.
I started trying with a dictionary but then got confused.
Or how would this look?
Sorry, brain is burnt out today.
I am setting each one up onto the grid using the following code:
self.header = customtkinter.CTkLabel(self, text=self.header_name) self.header.grid(row=0, column=0, padx=10, pady=10) self.radio_button_var = customtkinter.StringVar(value="") self.radio_button_1 = customtkinter.CTkRadioButton(self, text=graph_types[0][0], value=graph_types[0][1], variable=self.radio_button_var) self.radio_button_1.grid(row=1, column=0, padx=10, pady=10, sticky="W") self.radio_button_2 = customtkinter.CTkRadioButton(self, text=graph_types[1][0], value=graph_types[1][1], variable=self.radio_button_var) self.radio_button_2.grid(row=2, column=0, padx=10, pady=10, sticky="W") self.radio_button_3 = customtkinter.CTkRadioButton(self, text=graph_types[2][0], value=graph_types[2][1], variable=self.radio_button_var) self.radio_button_3.grid(row=3, column=0, padx=10, pady=10, sticky="W") self.radio_button_4 = customtkinter.CTkRadioButton(self, text=graph_types[3][0], value=graph_types[3][1], variable=self.radio_button_var) self.radio_button_4.grid(row=4, column=0, padx=10, pady=10, sticky="W") self.radio_button_5 = customtkinter.CTkRadioButton(self, text=graph_types[4][0], value=graph_types[4][1], variable=self.radio_button_var) self.radio_button_5.grid(row=5, column=0, padx=10, pady=10, sticky="W") self.radio_button_6 = customtkinter.CTkRadioButton(self, text=graph_types[5][0], value=graph_types[5][1], variable=self.radio_button_var) self.radio_button_6.grid(row=6, column=0, padx=10, pady=10, sticky="W") self.radio_button_7 = customtkinter.CTkRadioButton(self, text=graph_types[6][0], value=graph_types[6][1], variable=self.radio_button_var) self.radio_button_7.grid(row=7, column=0, padx=10, pady=10, sticky="W")This continues for some time.
As you can see, all I am doing is altering the position and value of each radio button. So, these lines are almost identical.
I know I should be able to loop over two lines and create the same concept but, for the life of me, I cannot see how.
I started trying with a dictionary but then got confused.
graph_types
is a 2D list that could easily become a dictionary; with the key being each radio button's name/number but is there a better way?Or how would this look?
Sorry, brain is burnt out today.