Home > Mobile >  customtkinter Radiobutton does not change IntVar value
customtkinter Radiobutton does not change IntVar value

Time:01-18

I try to run the following code. The main issue is, that I don't understand why when I make a different radiobutton active it doesn't change the entry placeholder_text.

I expected self.choice_var value to change, but it does not. I thought that .mainloop() actually should ensure the repetitiveness of the code, but it seems I get it wrong.

The main idea is to make a number of a different radiobutton options. Each option has to have a different amount of entry boxes shown. At this stage, I have two radiobuttons and two entry boxes.

As far as my knowledge allows, the code at this moment only changes the placeholder_text. The empty entry box with None should be inactive (not yet implemented), but ideally not to be shown at all (to figure out how).

But as I already marked it: firstly I want to understand why the placeholder_text values in the entry boxes do not change? Please advise.

import tkinter as tk
import customtkinter as ctk

ctk.set_appearance_mode("light")
ctk.set_default_color_theme("blue")

class Interface(ctk.CTk):
    def __init__(self):
        super().__init__()

        # configure window
        self.title('Tool')
        self.geometry(f"{900}x{600}")

        # configure grid layout
        self.grid_columnconfigure((0, 1, 2), weight=0)
        self.grid_rowconfigure((0, 1, 2, 3, 4, 5), weight=0) 
        
        # configure radiobutton frame
        self.radiobutton_frame = ctk.CTkFrame(self, width=250)
        self.radiobutton_frame.grid(row=0, column=0, padx=20, pady=20, sticky='nw')
        self.radiobutton_label = ctk.CTkLabel(master=self.radiobutton_frame, text='Choose option:')
        self.radiobutton_label.grid(row=0, column=0, padx=10, pady=0, sticky='')
        
        self.choice_var = tk.IntVar()
        
        def selected_param_entry():
            return ('Param 1.1', 'Param 1.2') if self.choice_var.get() == 0 else ('Param 2.1', None)

        param1, param2 = selected_param_entry()

        self.first_option_btn = ctk.CTkRadioButton(master=self.radiobutton_frame, text='Option 1', variable=self.choice_var, value=0, command=selected_param_entry)
        self.first_option_btn.grid(row=1, column=0, padx=10, pady=5, sticky='n')
        self.second_option_btn = ctk.CTkRadioButton(master=self.radiobutton_frame, text='Option 2', variable=self.choice_var, value=1, command=selected_param_entry)
        self.second_option_btn.grid(row=2, column=0, padx=10, pady=5, sticky='n')

        # configure entry frame
        self.entry_frame = ctk.CTkFrame(self, width=250)
        self.entry_frame.grid(row=3, column=0, padx=20, pady=20, sticky='sw')
        self.entry_label = ctk.CTkLabel(master=self.entry_frame, text='Parameters: ')
        self.entry_label.grid(row=3, column=0, padx=10, pady=0, sticky='')
        
        self.entry_param1_entry = ctk.CTkEntry(self.entry_frame, placeholder_text=param1)
        self.entry_param1_entry.grid(row=4, column=0, padx=20, pady=5, sticky='sw')
        self.entry_param2_entry = ctk.CTkEntry(self.entry_frame, placeholder_text=param2)
        self.entry_param2_entry.grid(row=5, column=0, padx=20, pady=0, sticky='sw')

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

CodePudding user response:

There is no automatic link between the placeholder text of either CTkEntry components and the radio buttons, you need to implement it.

Try this instead, this code dynamically configures CTkEntry's placeholder from within the callback function given to the radio buttons:

def selected_param_entry():
    param1, param2 = ('Param 1.1', 'Param 1.2') if self.choice_var.get() == 0 else ('Param 2.1', '')
    self.entry_param1_entry.configure(placeholder_text=param1)
    self.entry_param2_entry.configure(placeholder_text=param2)

self.first_option_btn = ctk.CTkRadioButton(master=self.radiobutton_frame, text='Option 1', variable=self.choice_var, value=0, command=selected_param_entry)
self.first_option_btn.grid(row=1, column=0, padx=10, pady=5, sticky='n')
self.second_option_btn = ctk.CTkRadioButton(master=self.radiobutton_frame, text='Option 2', variable=self.choice_var, value=1, command=selected_param_entry)
self.second_option_btn.grid(row=2, column=0, padx=10, pady=5, sticky='n')

# configure entry frame
self.entry_frame = ctk.CTkFrame(self, width=250)
self.entry_frame.grid(row=3, column=0, padx=20, pady=20, sticky='sw')
self.entry_label = ctk.CTkLabel(master=self.entry_frame, text='Parameters: ')
self.entry_label.grid(row=3, column=0, padx=10, pady=0, sticky='')

self.entry_param1_entry = ctk.CTkEntry(self.entry_frame)
self.entry_param1_entry.grid(row=4, column=0, padx=20, pady=5, sticky='sw')
self.entry_param2_entry = ctk.CTkEntry(self.entry_frame)
self.entry_param2_entry.grid(row=5, column=0, padx=20, pady=0, sticky='sw')

selected_param_entry()

(anything above selected_param_entry() doesn't change (except maybe f"{900}x{600}" which could be advantageously written '900x600')

  • Related