我用 frame.winfo_children()
得到了框架的小部件列表。现在我需要获取该复选按钮的值。子列表:
[<tkinter.Label object .!toplevel2.!labelframe.!label>, <tkinter.Entry object .!toplevel2.!labelframe.!entry>, <tkinter.Entry object .!toplevel2.!labelframe.!entry2>, <tkinter.Checkbutton object .!toplevel2.!labelframe.!checkbutton>]
我试过的:
checkbuttonwidget.get()
, checkbuttonwidget.cget('变量')
如何获取它的值?是否有任何选项可以使用 if
命令来拆分这些 child ?
例如:
for wid in frame.winfo_children():
if wid == LABEL(option?):
print('yes its a label')
elif wid == CheckButton(option?):
print('Its a CheckButton')
最佳答案
检查小部件是否为 Checkbutton
的最简单方法是使用 isinstance
,然后要获取值,您可以使用 .getvar
和 widget['variable']
(或 widget.cget('variable')
取决于你喜欢什么)来获取相应变量的值:
for widget in frame.winfo_children():
if isinstance(widget, tk.Checkbutton):
value = frame.getvar(widget['variable'])
print(value)
另外,根据您导入的方式,您可能需要使用 Checkbutton
而不是 tk.Checkbutton
有用的:
- >
getvar(self, name='PY_VAR') Return value of Tcl variable NAME.
关于Python Tkinter 从子列表中获取 CheckButton 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69868325/