Anonymous
Tkinter выбрать все кнопки проверки
Сообщение
Anonymous » 20 фев 2026, 23:46
Код: Выделить всё
import tkinter as tk
from tkinter import ttk
def select_all(event):
checkbutton_1_int.set(not checkbutton_all_int.get())
checkbutton_2_int.set(not checkbutton_all_int.get())
checkbutton_3_int.set(not checkbutton_all_int.get())
checkbutton_4_int.set(not checkbutton_all_int.get())
def unselect_all(event):
if checkbutton_1_int.get() == 0 or checkbutton_1_int.get() == 0:
checkbutton_all_int.set(0)
if checkbutton_1_int.get() == 1 and checkbutton_1_int.get() == 1:
checkbutton_all_int.set(1)
root = tk.Tk()
root.geometry("300x300")
checkbutton_all_int = tk.IntVar()
checkbutton_all_int.set(0)
checkbutton_1_int = tk.IntVar()
checkbutton_1_int.set(0)
checkbutton_2_int = tk.IntVar()
checkbutton_2_int.set(0)
checkbutton_3_int = tk.IntVar()
checkbutton_3_int.set(0)
checkbutton_4_int = tk.IntVar()
checkbutton_4_int.set(0)
checkbutton_all = ttk.Checkbutton(text="all",variable=checkbutton_all_int, onvalue=1, offvalue=0)
checkbutton_1 = ttk.Checkbutton(text="1",variable=checkbutton_1_int, onvalue=1, offvalue=0)
checkbutton_2 = ttk.Checkbutton(text="2",variable=checkbutton_2_int, onvalue=1, offvalue=0)
checkbutton_3 = ttk.Checkbutton(text="3",variable=checkbutton_3_int, onvalue=1, offvalue=0)
checkbutton_4 = ttk.Checkbutton(text="4",variable=checkbutton_4_int, onvalue=1, offvalue=0)
checkbutton_all.pack()
checkbutton_1.pack()
checkbutton_2.pack()
checkbutton_3.pack()
checkbutton_4.pack()
checkbutton_all.bind("",select_all)
checkbutton_1.bind("",unselect_all)
checkbutton_2.bind("",unselect_all)
checkbutton_3.bind("",unselect_all)
checkbutton_4.bind("",unselect_all)
root.mainloop()
Как я могу сделать так, чтобы при снятии флажка 1, 2, 3 или 4 все кнопки также снимались?
Я не понимаю, почему он снимает флажки со всех кнопок, когда я деактивирую и снова активирую кнопку 1 (или любую другую).
Мне кажется, что текущее значение кнопки проверки всегда на единицу позади.
Подробнее здесь:
https://stackoverflow.com/questions/798 ... eckbuttons
1771620364
Anonymous
[code]import tkinter as tk from tkinter import ttk def select_all(event): checkbutton_1_int.set(not checkbutton_all_int.get()) checkbutton_2_int.set(not checkbutton_all_int.get()) checkbutton_3_int.set(not checkbutton_all_int.get()) checkbutton_4_int.set(not checkbutton_all_int.get()) def unselect_all(event): if checkbutton_1_int.get() == 0 or checkbutton_1_int.get() == 0: checkbutton_all_int.set(0) if checkbutton_1_int.get() == 1 and checkbutton_1_int.get() == 1: checkbutton_all_int.set(1) root = tk.Tk() root.geometry("300x300") checkbutton_all_int = tk.IntVar() checkbutton_all_int.set(0) checkbutton_1_int = tk.IntVar() checkbutton_1_int.set(0) checkbutton_2_int = tk.IntVar() checkbutton_2_int.set(0) checkbutton_3_int = tk.IntVar() checkbutton_3_int.set(0) checkbutton_4_int = tk.IntVar() checkbutton_4_int.set(0) checkbutton_all = ttk.Checkbutton(text="all",variable=checkbutton_all_int, onvalue=1, offvalue=0) checkbutton_1 = ttk.Checkbutton(text="1",variable=checkbutton_1_int, onvalue=1, offvalue=0) checkbutton_2 = ttk.Checkbutton(text="2",variable=checkbutton_2_int, onvalue=1, offvalue=0) checkbutton_3 = ttk.Checkbutton(text="3",variable=checkbutton_3_int, onvalue=1, offvalue=0) checkbutton_4 = ttk.Checkbutton(text="4",variable=checkbutton_4_int, onvalue=1, offvalue=0) checkbutton_all.pack() checkbutton_1.pack() checkbutton_2.pack() checkbutton_3.pack() checkbutton_4.pack() checkbutton_all.bind("",select_all) checkbutton_1.bind("",unselect_all) checkbutton_2.bind("",unselect_all) checkbutton_3.bind("",unselect_all) checkbutton_4.bind("",unselect_all) root.mainloop() [/code] Как я могу сделать так, чтобы при снятии флажка 1, 2, 3 или 4 все кнопки также снимались? Я не понимаю, почему он снимает флажки со всех кнопок, когда я деактивирую и снова активирую кнопку 1 (или любую другую). Мне кажется, что текущее значение кнопки проверки всегда на единицу позади. Подробнее здесь: [url]https://stackoverflow.com/questions/79893215/tkinter-select-all-checkbuttons[/url]