'''
Created on 2017年9月8日
@author: Nick
'''
'''Tkinter之Checkbutton篇'''
#_*_coding:utf-8_*_
import tkinter as tk
from tkinter import *
#回调函数
def callCheckbutton():
print('Your check this button!')
#回调函数,改变v的值,即改变Checkbutton的显示值
def changeCheckbuttoText():
e.set('Tkinker')
if __name__ == '__main__':
root = tk.Tk()
root.wm_title('Checkbutton')
root.geometry("1800x800+120+100") #设置窗口大小 并初始化桌面位置
root.resizable(width = True,height = True) #宽不可变 高可变 默认True
#1、设置Checkbutton的回调函数
# Checkbutton又称为多选按钮,可以表示两种状态:On和Off,可以设置回调函数,每当点击此按钮时回调函数被调用
# 不管Checkbutton的状态如何,此回调函数都会被调用
fram1 = Frame(root)
Checkbutton(fram1,text = 'python').pack(side = LEFT)
Checkbutton(fram1,text = 'python1',command = callCheckbutton).pack(side = LEFT)
fram1.pack(side = TOP)
#2、通过回调函数改变Checkbutton的显示文本text的值
fram2 = Frame(root)
e = StringVar()
e.set('python2')
#绑定v到Checkbutton的属性textvariable
cb = Checkbutton(fram2, text = 'python2',textvariable = e,command = changeCheckbuttoText)
cb.focus_set()
cb.pack(side = LEFT)
fram2.pack(side = TOP)
#3、上述的textvariable使用方法与Button的用法完全相同,使用此例是为了区别Checkbutton的另外的一个属性variable,
# 此属性与textvariable不同,它是与这个控件本身绑定,Checkbutton自己有值:On和Off值,缺省状态On为1,Off为0,
fram3 = Frame(root)
#将一整数与Checkbutton的值绑定,每次点击Checkbutton,将打印出当前的值
def changeCheckbutton():
print(e.get())
e = IntVar()
Checkbutton(fram3,text = 'python3',variable = e,command = changeCheckbutton).pack(side = LEFT)
fram3.pack(side = TOP)
#4、Checkbutton的值不仅仅是1或0,可以是其他类型的数值,可以通过onvalue和offvalue属性设置Checkbutton的状态值,
# 如下代码将On设置为'python',Off值设置为'Tkinter',程序的打印值将不再是0或1,而是'Tkinter’或‘python
fram4 = Frame(root)
def changCheckbutton_onoff():
print(e.get())
e = StringVar()
Checkbutton(fram4,text = 'python4',
variable = e,
onvalue = 'CheckOn', #设置On的值
offvalue = 'CheckOff', #设置Off的值
command = changCheckbutton_onoff).pack(side = LEFT)
fram4.pack(side = TOP)
#6、还有其他的属性fg/bg/relief/width/height/justify/state使用方法与Button相同
fram5 = Frame(root)
# 设置前景色、背景色fg/bg
Checkbutton(fram5,text = 'python5',fg = 'red').pack(side = LEFT)
Checkbutton(fram5,text = 'python6',bg = 'green').pack(side = LEFT)
# 设置宽度,高度
Checkbutton(fram5,text = 'python7',
fg = 'yellow',
bg = 'blue',
width = 20,height = 3).pack(side = LEFT)
fram5.pack(side = TOP)
# 设置relief属性文本框风格,如凹陷、凸起,值有:flat/sunken/raised/groove/ridge
fram6 = Frame(root)
Checkbutton(fram6,text = 'python8',fg = 'yellow',relief = 'flat').pack(side = LEFT)
Checkbutton(fram6,text = 'python9',fg = 'red',relief = 'sunken').pack(side = LEFT)
Checkbutton(fram6,text = 'python10',fg = 'green',relief = 'raised').pack(side = LEFT)
Checkbutton(fram6,text = 'python11',fg = 'blue',relief = 'groove').pack(side = LEFT)
Checkbutton(fram6,text = 'python12',fg = 'purple',relief = 'ridge').pack(side = LEFT)
fram6.pack(side = TOP)
#设置Checkbutton状态state = normal/active/disabled
fram7 = Frame(root)
Checkbutton(fram7,text = 'python13',fg = 'yellow',state = 'normal').pack(side = LEFT)
Checkbutton(fram7,text = 'python14',fg = 'red',state = 'active').pack(side = LEFT)
Checkbutton(fram7,text = 'python15',fg = 'green',state = 'disabled').pack(side = LEFT)
fram7.pack(side = TOP)
root.mainloop()
Tkinter之Checkbutton篇
最新推荐文章于 2022-07-18 11:00:33 发布