Как сделать checkbox по умолчанию отмеченным в tKinter
хочу сделать чтобы по умолчанию было установлено несколько checkbox. пробовал через offvalue и onvalue. но не прошло.
Вот код:
from tkinter import *
import os
win = Tk()
win.title("win")
win.geometry("300x265")
win["bg"] = "gray6"
# Browsers:
def Install():
if (Chrome.get()):
os.system('Start Chrome.exe')
if (Yandex.get()):
os.system('Start Yandex.exe')
if (FireFox.get()):
os.system('Start FireFox.exe')
if (winRAR.get()):
os.system('Start winRAR.exe')
if (Zip.get()):
os.system('Start 7z.exe')
if (MicrosoftOffice.get()):
os.system('Start MicrosoftOffice.exe')
if (LibreOffice.get()):
os.system('Start LibreOffice.exe')
if (uTorrent.get()):
os.system('Start uTorrent.exe')
if (qBitTorrent.get()):
os.system('Start qbittorrent.exe')
if (DriverBooster.get()):
os.system('Start DriverBooster.exe')
if (UninstallTool.get()):
os.system('Start UninstallTool.exe')
if (Notepad.get()):
os.system('Start Notepad.exe')
if (Picasa.get()):
os.system('Start Picasa.exe')
if (MiniBin.get()):
os.system('Start MiniBin.exe')
if (EarTrumpet.get()):
os.system('Start EarTrumpet.exe')
if (Everything.get()):
os.system('Start Everything.exe')
if (Lightshot.get()):
os.system('Start Lightshot.exe')
if (Flux.get()):
os.system('Start Flux.exe')
if (Battery.get()):
os.system('Start Battery.exe')
if (Tach_B590.get()):
os.system('Start Tach_B590.exe')
Browser = Label(win,
text='Browser',
bg="gray6",
fg="red")\
.place(x=1, y=1)
Chrome = BooleanVar()
ChromeC = Checkbutton(win,
text='Chrome',
variable=Chrome,
bg="gray6",
fg="green3",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=10, y=20)
Yandex = BooleanVar()
YandexC = Checkbutton(win,
text='Yandex',
variable=Yandex,
bg="gray6",
fg="green3",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=10, y=39)
FireFox = BooleanVar()
FireFoxC = Checkbutton(win,
text='FireFox',
variable=FireFox,
bg="gray6",
fg="green3",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=10, y=58)
# Archiver:
Archiver = Label(win,
text='Archiver',
bg="gray6",
fg="red")\
.place(x=1, y=77)
winRAR = BooleanVar()
winRARC = Checkbutton(win,
text='winRAR',
variable=winRAR,
bg="gray6",
fg="green3",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=10, y=96)
Zip = BooleanVar()
ZipC = Checkbutton(win,
text='7zip',
variable=Zip,
bg="gray6",
fg="green3",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=10, y=115)
# Office:
Office = Label(win,
text='Office',
bg="gray6",
fg="red")\
.place(x=1, y=134)
MicrosoftOffice = BooleanVar()
MicrosoftOfficeC = Checkbutton(win,
text='MicrosoftOffice',
variable=MicrosoftOffice,
bg="gray6",
fg="green3",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=10, y=153)
LibreOffice = BooleanVar()
LibreOfficeC = Checkbutton(win,
text='LibreOffice',
variable=LibreOffice,
bg="gray6",
fg="green3",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=10, y=172)
# Torrent:
TorrentC = Label(win,
text='Torrent',
bg="gray6",
fg="red")\
.place(x=1, y=191)
uTorrent = BooleanVar()
uTorrentC = Checkbutton(win,
text='uTorrent',
variable=uTorrent,
bg="gray6",
fg="green3",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=10, y=210)
qBitTorrent = BooleanVar()
qBitTorrentC = Checkbutton(win,
text='qBitTorrent',
variable=qBitTorrent,
bg="gray6",
fg="green3",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=10, y=229)
DriverBooster = BooleanVar()
DriverBoosterC = Checkbutton(win,
text='DriverBooster',
variable=DriverBooster,
bg="gray6",
fg="cyan2",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=150, y=1)
UninstallTool = BooleanVar()
UninstallToolC = Checkbutton(win,
text='UninstallTool',
variable=UninstallTool,
bg="gray6",
fg="cyan2",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=150, y=20)
Notepad = BooleanVar()
NotepadC = Checkbutton(win,
text='Notepad++',
variable=Notepad,
bg="gray6",
fg="cyan2",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=150, y=39)
Picasa = BooleanVar()
PicasaC = Checkbutton(win,
text='Picasa',
variable=Picasa,
bg="gray6",
fg="cyan2",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=150, y=58)
MiniBin = BooleanVar()
MiniBinC = Checkbutton(win,
text='MiniBin',
variable=MiniBin,
bg="gray6",
fg="cyan2",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=150, y=77)
EarTrumpet = BooleanVar()
EarTrumpetC = Checkbutton(win,
text='EarTrumpet',
variable=EarTrumpet,
bg="gray6",
fg="cyan2",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=150, y=96)
Everything = BooleanVar()
EverythingC = Checkbutton(win,
text='Everything',
variable=Everything,
bg="gray6",
fg="cyan2",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=150, y=115)
Lightshot = BooleanVar()
LightshotC = Checkbutton(win,
text='Lightshot',
variable=Lightshot,
bg="gray6",
fg="cyan2",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=150, y=134)
Flux = BooleanVar()
FluxC = Checkbutton(win,
text='Flux',
variable=Flux,
bg="gray6",
fg="cyan2",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=150, y=153)
# for NoteBook:
Battery = BooleanVar()
BatteryC = Checkbutton(win,
text='Battery',
variable=Battery,
bg="gray6",
fg="seagreen2",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=150, y=191)
Tach_B590 = BooleanVar()
Tach_B590C = Checkbutton(win,
text='Tach_B590',
variable=Tach_B590,
bg="gray6",
fg="seagreen2",
activebackground="gray6",
activeforeground="green3",
selectcolor="gray6",
)\
.place(x=150, y=210)
Install = Button(win,
text='Install',
bg="gray6",
fg="khaki1",
bd="0",
activebackground="gray6",
activeforeground="lawn green",
width="4",
height="1",
command=Install)\
.place(x=250, y=237)
win.mainloop()
Ответы (2 шт):
Автор решения: Frehzy
→ Ссылка
По аналогии с ответом по ссылке https://stackoverflow.com/q/37595078/13123688
CheckVar = IntVar(value=1)
ChromeC = Checkbutton(... , variable=CheckVar)
или
ChromeC.toggle()
или
ChromeC.state(["selected"])

