[英]Radiobutton instance has no attibute 'variable
I'm trying to give the user a set of choices using radio buttons. 我正在尝试使用单选按钮为用户提供一组选择。 When I click on the "Get Data Type" button, Python returns "AttributeError: Radiobutton instance has no attribute 'variable'. Clearly it does, but refuses to see it (them):
当我单击“获取数据类型”按钮时,Python返回“ AttributeError:单选按钮实例没有属性'variable'。显然它有,但是拒绝看到它们(它们):
class DataType:
def __init__(self, master):
frame = LabelFrame(master, text = 'Data Type')
frame.pack()
data_contents = StringVar()
self.radiobutton = Radiobutton(frame,
text="Fixed Data",
variable = data_contents,
value = 'fixed')
self.radiobutton.pack()
self.radiobutton = Radiobutton(frame,
text="Random Data",
variable = data_contents,
value = 'random')
self.radiobutton.pack()
self.radiobutton = Radiobutton(frame,
text="All 1's",
variable = data_contents,
value = '1s')
self.radiobutton.pack()
self.radiobutton = Radiobutton(frame,
text="All 0's",
variable = data_contents,
value = '0s')
self.radiobutton.pack()
data_contents.set(self.radiobutton)
self.printdata = Button(frame,
text="What data?",
command=self.write_data)
self.printdata.pack()
def write_data(self):
print (self.radiobutton.variable)
You are misunderstanding tkinter options. 您对tkinter选项有误解。 When you specify an attribute like
variable=data_contents
, it does not become an attribute of the object. 当您指定诸如
variable=data_contents
类的属性时,它不会成为对象的属性。 Instead, it is a widget configuration option. 相反,它是一个小部件配置选项。
To access the value of the variable
configuration option you need to use one of the following: 要访问
variable
配置选项的值,您需要使用以下之一:
self.radiobutton.cget("variable")
self.radiobutton["variable"]
Note: you are headed down the wrong path with this question. 注意:这个问题使您走错了路。 You don't need to get the variable since you control what the variable is.
因为您控制变量是什么,所以不需要获取变量。 The variable should be an instance attribute that you can call the
get()
method on: 该变量应该是实例属性,您可以在以下属性上调用
get()
方法:
...
self.data_contents = StringVar()
...
def write_data(self):
print(self.data_contents.get())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.