一、參數說明
| 語法 | 作用 |
|---|---|
| Checkbutton(root,text='xxxx') | 復選框顯示的文本 |
| Checkbutton(root,variable=id) | 通過變量的值確定哪些復選框被選中 |
| Checkbutton(root,variable=id,onvalue=1) | 復選框選中(有效)時變量的值 |
| Checkbutton(root,variable=id,onvalue=1,offvalue=0) | 復選框未選中(無效)時變量的值 |
| Checkbutton(root,variable=id,onvalue=1,offvalue=0,command=函數) | 復選框選中時執行的命令(函數) |
二、代碼示例
import tkinter as tk
window = tk.Tk()
def main():
global window
window.title("CheckButton參數說明")
winWidth = 600
winHeight = 400
# 獲取屏幕寬高
screenWidth = window.winfo_screenwidth()
screenHeight = window.winfo_screenheight()
x = int((screenWidth - winWidth) / 2)
y = int((screenHeight - winHeight) / 2)
# 設置窗口居中
window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
window.iconbitmap("./image/icon.ico")
"""checkbutton參數.
Valid resource names: activebackground, activeforeground, anchor,
background, bd, bg, bitmap, borderwidth, command, cursor,
disabledforeground, fg, font, foreground, height,
highlightbackground, highlightcolor, highlightthickness, image,
indicatoron, justify, offvalue, onvalue, padx, pady, relief,
selectcolor, selectimage, state, takefocus, text, textvariable,
underline, variable, width, wraplength."""
var1 = tk.IntVar()
var2 = tk.IntVar()
tk.Checkbutton(window, text="籃球", onvalue=1, offvalue=0,
variable=var1, command=callBack, activebackground="#f00",
foreground="#666").pack()
tk.Checkbutton(window, text="足球", onvalue=1, offvalue=0,
variable=var2, command=callBack, activebackground="#f00",
foreground="#bbb").pack()
window.mainloop()
def callBack():
print(1)
if __name__ == '__main__':
main()
三、效果圖

