我正在嘗試使用 breezypythongui 制作一個簡單的 Python GUI,但在添加單選按鈕時遇到了麻煩。我正在嘗試使用“addRadiobuttonGroup”方法/方法,但是每當我呼叫此方法時,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 上進行的全面搜索沒有發現任何資訊,所以我希望這里的人可能知道為什么我無法在沒有出現此錯誤的情況下將 radioButtons 添加到我的 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()
uj5u.com熱心網友回復:
問題是這一行:
self._commonVar = Tkinter.StringVar("")
第一個位置引數StringVar必須是一個小部件,但""它是一個字串而不是一個小部件。
如果您嘗試將初始值顯式設定為空字串,請將空字串分配給value引數:
self._commonVar = Tkinter.StringVar(value="")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/465747.html
上一篇:tkinter無法打開視窗的問題(Python tkinter模塊)
下一篇:我的應用程式沒有顯示在螢屏上
