一、参数说明
| 语法 | 作用 |
|---|---|
| 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()
三、效果图
