Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping over radio buttons
#5
I used @Yoriz approach, since it was closer to my original attempt; though this is also similar to the first suggestion, which I also tried.

When I attempted the first one, I encountered a problem that I thought this one would resolve, but it didn't.

The issue comes about in that I have 36 different graph types I need to consider. Having these in a single column is, therefore, not an option.

I introduced two variables, row and col, incrementing these accordingly (row by 1 each time, reset to 0 when row == 10 and incremented col by 1.

This worked nicely, with the code now looking like this...

		self.radio_buttons = []
		col = 0
		row = 0

		for graph_type in graph_types:
			text, value = graph_type
			radio_button = customtkinter.CTkRadioButton(self, text=text, value=value, variable=self.radio_button_var)
			radio_button.grid(row=row, column=col, padx=5, pady=5, sticky="W")
			self.radio_buttons.append(radio_button)
			row += 1
			if (row == 20):
				col += 1
				row = 0
Here's the issue, if I select one radio button then, on occasion, others are also selected?!?

This does not happen if I simply use the index as the row reference (though this runs off the page).

The references to each button in the list self.radio_buttons are unique and this is not changing. The relationship between the active buttons is not apparent ([5, 25, 27], [22, 36]) and, as you can see from that list, this only occurs for 5 buttons?!?

Any ideas?

Total code is here...

import customtkinter

graph_types = [["Tbol-age - tauir-age", "TLTAge"], ["Makes the coffee", "MtC"], ["Lbol - Lshock (Class 1 data)", "LL1"], ["Lbol - CO Momentum","LCM"], ["Tbol - Lbol","TL"], ["Menv - H2","MH2"], ["Lbol - Lshock (Class 0 data)","LL0"], ["Tbol - H2","TH2"], ["Mcor - Lbol + Molinari data + Elia data","MLME"]
, ["Probability Diagram","Prob"], ["Mstar - Tstar","MS_TS"], ["Mstar - Rstar","MS_RS"], ["Fco * x / Lbol vs Menv / Lbol^0.6","FLML"], ["Lbol - Tbol (combined)","LTC"], ["Mstar - Lbol","MS_L"], ["Mcor - Lbol + (M + E) - different axes","ML"], ["Mcor - Lbol ???","ML_Unknown"], ["TEST Time - Rstar","TEST1"], ["TEST Time - Lint","TEST2"], ["Menv - Lbol - L(Ly) / Lbol","MLLL"], ["Mstar - Lbol / Ledd","MLL"], ["Tstar - Lbol","TS_L"], ["Time - Tbol","TT"], ["Tbol - Mclump / Lbol","TML"], ["Tstar - Lint","TL"], ["Single histogram for L/M","SLM"], ["Time - Lbol","TL"], ["Tbol isotherms","TI"], ["HR Diagram for Lint and Lbol tracks","HR"], ["Mstar - vjet","MS_V"], ["Time - Lbol / Mcor","TLM"], ["Disk radial profiles","DRP"], ["Lbol - Lshock","LL"], [" Time - Mdot","TMdot"], ["Lbol - L(Ly)","LLLy"], ["Tstar - Lint","TS_L"]]

customtkinter.set_default_color_theme("dark-blue")  # Themes: "blue" (standard), "green", "dark-blue"
customtkinter.set_appearance_mode("system")			# Appearance: "system" (standard), "dark", "light"

class RadioButtonFrame(customtkinter.CTkFrame):
	
	def __init__(self, *args, header_name="RadioButtonFrame", **kwargs):
		super().__init__(*args, **kwargs)
        
		self.header_name = header_name

		self.header = customtkinter.CTkLabel(self, text=self.header_name)
		self.header.grid(row=0, column=0, padx=5, pady=5)

		self.radio_button_var = customtkinter.StringVar(value="")
 
		self.radio_buttons = []
		col = 0
		row = 0

		for graph_type in graph_types:
			text, value = graph_type
			radio_button = customtkinter.CTkRadioButton(self, text=text, value=value, variable=self.radio_button_var)
			radio_button.grid(row=row, column=col, padx=5, pady=5, sticky="W")
			self.radio_buttons.append(radio_button)
			row += 1
			if (row == 20):
				col += 1
				row = 0
				
		for button in self.radio_buttons:
			print (button)


class App(customtkinter.CTk):
	def __init__(self):
		super().__init__()

		self.geometry("1024x768")
		self.title("RadioButtonFrame test")

# create radio button frame 1 with print button
		self.radio_button_frame_1 = RadioButtonFrame(self, header_name="Graph Selections")
		self.radio_button_frame_1.grid(row=0, column=0, padx=20, pady=20)

		self.frame_1_button = customtkinter.CTkButton(self, text="Print value of frame", command=self.print_value_frame)
		self.frame_1_button.grid(row=1, column=0, padx=20, pady=10)

	def print_value_frame(self):
		print(f"Frame 1 value: {self.radio_button_frame_1.get_value()}")


if __name__ == "__main__":
	app = App()
	app.mainloop()

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
Looping over radio buttons - by garynewport - Jan-06-2023, 12:43 PM
RE: Looping over radio buttons - by carecavoador - Jan-06-2023, 02:43 PM
RE: Looping over radio buttons - by deanhystad - Jan-06-2023, 03:52 PM
RE: Looping over radio buttons - by Yoriz - Jan-06-2023, 06:45 PM
RE: Looping over radio buttons - by garynewport - Jan-07-2023, 12:47 PM
RE: Looping over radio buttons - by Yoriz - Jan-07-2023, 01:43 PM
RE: Looping over radio buttons - by garynewport - Jan-07-2023, 02:23 PM
RE: Looping over radio buttons - by deanhystad - Jan-07-2023, 04:32 PM
RE: Looping over radio buttons - by garynewport - Jan-10-2023, 10:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  packet radio communication EmpireAndrew 1 2,324 Nov-01-2019, 06:35 PM
Last Post: micseydel
  Internet radio script ahim 3 3,601 Jul-14-2018, 06:13 PM
Last Post: ahim
  Buttons or Radio Buttons on Tkinter Treeview draems 0 3,524 Oct-31-2017, 04:06 AM
Last Post: draems
  tkinter - Make circle for radio button bigger Raptor88 5 14,617 Mar-07-2017, 12:13 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020