breezypythongui addRadiobuttonGroup throwing unexpected AttributeError: 'str' object has no attribute '_root'

breezypythongui addRadiobuttonGroup throwing unexpected AttributeError: 'str' object has no attribute '_root'

我正在尝试使用 breezypythongui 制作一个简单的 Python GUI,但在添加单选按钮时遇到了问题。我正在尝试使用 'addRadiobuttonGroup' method/approach,但每当我调用此方法时,GUI 都会出错并显示:

 __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'

在 Internet 上进行的全面搜索没有显示任何内容,所以我希望这里的人可能知道为什么我无法在不出现此错误的情况下将单选按钮添加到我的 GUI。我的代码是:

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()

问题出在这一行:

self._commonVar = Tkinter.StringVar("")

StringVar 的第一个位置参数必须是小部件,但 "" 是字符串而不是小部件。

如果您尝试将初始值显式设置为空字符串,请将空字符串分配给 value 参数:

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