社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

breezypythongui AddRadioButtongGroup抛出意外的AttributeError:“str”对象没有属性“\u root”

Dog • 3 年前 • 1366 次点击  

我正在尝试使用breezypythongui制作一个简单的Python GUI,但在添加单选按钮时遇到了麻烦。我试图使用“AddRadioButtongGroup”方法/方法,但每当我调用此方法时,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'

在互联网上进行的全面搜索没有发现任何信息,所以我希望这里的人可能知道为什么我不能在不出现此错误的情况下将单选按钮添加到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()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132772
 
1366 次点击  
文章 [ 1 ]  |  最新文章 3 年前
Bryan Oakley
Reply   •   1 楼
Bryan Oakley    3 年前

问题在于这一行:

self._commonVar = Tkinter.StringVar("")

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

如果试图显式地将初始值设置为空字符串,请将空字符串指定给 value 论点:

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