可以使用 winfo_children() 方法来获取 Frame 中的所有子组件,然后使用 winfo_classname() 方法判断是否是 Checkbutton,再使用 get() 方法获取 Checkbutton 的值。下面是一个示例代码:

import tkinter as tk

def get_checkbutton_values():
    for child in frame.winfo_children():
        if child.winfo_classname() == 'Checkbutton':
            print(child.get())

root = tk.Tk()

frame = tk.Frame(root)
frame.pack()

checkbutton1 = tk.Checkbutton(frame, text='Checkbutton 1')
checkbutton1.pack()

checkbutton2 = tk.Checkbutton(frame, text='Checkbutton 2')
checkbutton2.pack()

checkbutton3 = tk.Checkbutton(frame, text='Checkbutton 3')
checkbutton3.pack()

button = tk.Button(root, text='Get Values', command=get_checkbutton_values)
button.pack()

root.mainloop()

运行以上代码,点击"Get Values"按钮,就会打印出每个 Checkbutton 的值。

Python Tkinter 遍历 Frame 获取 Checkbutton 值

原文地址: https://www.cveoy.top/t/topic/qmXK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录