Menu

StringVar for RadioButton Vaue

Anonymous
2023-07-09
2023-07-11
  • Anonymous

    Anonymous - 2023-07-09

    Hello,

    Thanks for this wonderful project. I am facing an issue with the RadioButton. I have four radio buttons defined and all are mapped to the single variable "trdFlavor1". How can I get the StringVar value for the variable instead of IntVar. (See attached screenshot). Right now I am manually modifying the generated python file to say
    self.trdFlavor1 = tk.StringVar()

    I tried setting textVar to trdFlavor1 but it creates multiple lines like this for each radiobutton.

      self.trdFlavor1 = tk.StringVar()
    

    Am I doing something wrong here?

    Thanks in anticipation!

    Have a great day!

     
  • Greg Walters

    Greg Walters - 2023-07-09

    Many years ago, the Radiobuttons were able to take a StringVar or an IntVar depending on the value given to the widget. However, recently we went with using an IntVar internally, in an attempt to keep confusion out of the coding process. Can you image if you had your four TRadiobuttons three with StringVars and one with an IntVar, but pointed to the same variable? It would get fairly ugly.

    So, you are stuck with IntVars for the value attribute.

    However, it isn't all bad news. Set the Value to 1 for the first, increment the value for each of the others. You can keep the Variable the same for all (defaults to "selectbutton" and use the same command callback for all four.

    Then the callback can handle thing easily with an if|elif tree...

    def on_TRadioclick(*args):
        if _debug:
            print("rbtn1_support.on_TRadioclick")
            for arg in args:
                print("    another arg:", arg)
            sys.stdout.flush()
        which = _w1.trdFlavor1.get()
        print(which)
        if which == 1:
            print("Pizza")
        elif which == 2:
            print("Mushrooms")
        elif which == 3:
            print("Spinich")
        elif which == 4:
            print("Ice Cream")
    

    I hope this helps.
    Greg

     
  • Anonymous

    Anonymous - 2023-07-11

    Makes sense. Thanks for the clarification!

     

Anonymous
Anonymous

Add attachments
Cancel