Home > front end >  breezypythongui addRadiobuttonGroup throwing unexpected AttributeError: 'str' object has n
breezypythongui addRadiobuttonGroup throwing unexpected AttributeError: 'str' object has n

Time:04-24

I'm trying to make a simple Python GUI using breezypythongui, but have been having trouble adding radio buttons. I'm trying to use the 'addRadiobuttonGroup' method/approach, but whenever I call this method, the GUI errors out and states:

 __init__
    self._commonVar = Tkinter.StringVar("")
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 540, in __init__
    Variable.__init__(self, master, value, name)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 372, in __init__
    self._root = master._root()
AttributeError: 'str' object has no attribute '_root'

A comprehensive search on the Internet has revealed nothing, so I'm hoping someone here might have an idea why I'm not able to add radioButtons to my GUI without getting this error. The code I have is:

from breezypythongui import EasyFrame
from tkinter import HORIZONTAL

class RadioButtonDemo(EasyFrame):
    """When the Display button is pressed, shows the label
    of the selected radio button.  The button group has a
    horizontal alignment."""

    def __init__(self):
        """Sets up the window and widgets."""
        EasyFrame.__init__(self, "Radio Button Demo")

        # Add the button group
        self.group = self.addRadiobuttonGroup(row=1, column=0,
                                              columnspan=4,
                                              orient=HORIZONTAL)

        # Add the radio buttons to the group
        self.group.addRadiobutton("Freshman")
        self.group.addRadiobutton("Sophomore")
        self.group.addRadiobutton("Junior")
        defaultRB = self.group.addRadiobutton("Senior")

        # Select one of the buttons in the group
        self.group.setSelectedButton(defaultRB)

        # Output field and command button for the results
        self.output = self.addTextField("", row=0, column=1)
        self.addButton("Display", row=0, column=0,
                       command=self.display)

    # Event handling method

    def display(self):
        """Displays the selected button's label in the text field."""
        self.output.setText(self.group.getSelectedButton()["value"])


def main():
    RadioButtonDemo().mainloop()


if __name__ == "__main__":
    main()

CodePudding user response:

The problem is this line:

self._commonVar = Tkinter.StringVar("")

The first positional argument to StringVar must be a widget, but "" is a string rather than a widget.

If you're trying to explicitly set the initial value to the empty string, assign the empty string to the value argument:

self._commonVar = Tkinter.StringVar(value="")
  • Related