I am trying to build my first GUI using tkinter for a project I am working on at work. I have multiple pairs of checkbuttons (labeled Rule and Trace in example below) that are dependent on each other. By default, all Rule checkbuttons are selected while all Trace checkbuttons are unselected. If I want to turn off a particular Rule checkbutton, it should turn red indicating it is unselected (or turned off). And if clicked again, it should turn green indicating it is selected (or turned on). I have written a function checkbutton_state to handle just that. Below is a working example of what I am trying to do. But my problem is, this function is applicable only for Rule 1 ON/OFF checkbutton. How can I generalize it so that I can use the same function for all the Rule checkbuttons?
Also, when a Particular rule is turned off, its corresponding Trace checkbutton should be turned off automatically and user should not be able to turn it on. If the rule checkbutton is turned on, then the corresponding checkbutton should be turned off but user should be able to turn it on if required.
I have tried using the 'lambda' method to try and generalize the function but not work. For automatically changing the status of the 'Trace' checkbutton, I have tried using the toggle() function but somehow its not giving me the desired result. I have been trying to find a solution to this the entire day. Any guidance on how I can proceed with this would be really appreciated.
My Python version is 3.7.1 and OS is Windows 10 (64 bit).
# Import requrired libraries/packages
from tkinter import Tk, Checkbutton, IntVar
# Create intance of tkinter
root = Tk()
# Define variables
rule1_on_choice = IntVar()
rule1_trace_choice = IntVar()
rule2_on_choice = IntVar()
rule2_trace_choice = IntVar()
# Create first set of checkbutton
# The 'trace' checkbutton is dependent on the 'on' checkbutton
# If the 'on' checkbutton is turned ON, 'trace' checkbutton should be turned off but user should be able to turn it on again
# If the 'on' checkbutton is turned OFF, 'trace' checkbutton should also be turned off but user should not be able to turn it on
rule1_on_checkbutton = Checkbutton(root, text = "Rule 1 ON", indicatoron = 0, bg = "green", variable = rule1_on_choice, onvalue = 1, offvalue = 0, selectcolor = "green")
rule1_trace_checkbutton = Checkbutton(root, text = "Rule 1 Trace", indicatoron = 0, bg = "red", variable = rule1_trace_choice, onvalue = 1, offvalue = 0, selectcolor = "red")
# Create second set of checkbuttons
rule2_on_checkbutton = Checkbutton(root, text = "Rule 2 ON", indicatoron = 0, bg = "green", variable = rule2_on_choice, onvalue = 1, offvalue = 0, selectcolor = "green")
rule2_trace_checkbutton = Checkbutton(root, text = "Rule 2 Trace", indicatoron = 0, bg = "red", variable = rule2_trace_choice, onvalue = 1, offvalue = 0, selectcolor = "red")
# The 'on' checkbuttons are turned on by default
# The 'trace' checkbuttons are turned off by default
rule1_on_checkbutton.select()
rule1_trace_checkbutton.deselect()
rule2_on_checkbutton.select()
rule2_trace_checkbutton.deselect()
rule1_on_checkbutton.pack()
rule1_trace_checkbutton.pack()
rule2_on_checkbutton.pack()
rule2_trace_checkbutton.pack()
# Function to change text and color of checkbutton
# If a Rule checkbutton is clicked, should turn green whith text showing "ON"
# The same checkbutton is clicked, it should now turn red with text showing "OFF"
def checkbutton_state(event = None):
if rule1_on_choice.get() == 1:
rule1_on_checkbutton.configure(text = "Rule 1 OFF", bg = "red", fg = "grey")
else:
rule1_on_checkbutton.configure(text = "Rule 1 ON", bg = "green", fg = "white")
# Binding function to Rule 1 'on' checkbutton
rule1_on_checkbutton.bind("<Button-1>", checkbutton_state)
root.mainloop()