#tkinter之Checkbutton篇

#Checkbutton又称为多选按钮,可以表示两种状态,On和Off,可以设置回调函数,每当点击此按钮时回调函数被调用。

1、一个简单的Checkbutton例子

 #创建一个Checkbutton,显示文本为'python'
from tkinter import * root = Tk()
Checkbutton(root,text='python').pack()
root.mainloop()

2、设置Checkbutton的回调函数

 from tkinter import *

 def callCheckbutton():
print('you check this button') root = Tk()
Checkbutton(root,text='chech python',command=callCheckbutton).pack()
root.mainloop()
#不管Checkbutton的状态如何,此回调函数都会被调用,即对话框你选或者不选,只要有点击,就会调用函数

3、通过回调函数改变Checkbutton的显示文本text的值

 from tkinter import *

 def callCheckbutton():
#改变v的值,即改变Checkbutton的显示值
v.set('Check Tkinter') root = Tk()
v = StringVar()#声明v是tkinter里面的一个字符串变量
v.set('good good')#设定字符串变量v的值
#绑定v到Checkbutton的属性textvariable
Checkbutton(root,textvariable =v,command=callCheckbutton).pack()
root.mainloop()

4、上述的textvariable使用方法与Button的用法完全相同,使用此例是为了区别Checkbutton的另外一个属性variable,此属性与textvariable不同,

它是与这个控件本身绑定,checkbutton本身有值,On与Off分别为1和0,

也就是说,variable的值是Checkbutton本身的值,是0或1等其他类型的数值

textvariable是里面显示的文本的值

 #显示Checkbutton的值
from tkinter import *
root =Tk()
#将一整数与Checkbutton的值绑定,每次点击Checkbutton,将打印当前的值
v = IntVar()#声明v是tkinter里面的一个整形变量 Checkbutton(root,variable = v,text ='checkbutton value').pack()
Label(root,textvariable = v).pack() root.mainloop()

5、Checkbutton的值不仅仅是1或者0,可以是其他类型的数值,可以通过onvalue,offvalue属性设置状态值

如下代码将On设置为python,Off设置为tkinter,程序打印值将不再是0或者1,而是tkinter和python

 from tkinter import *
root = Tk()
#将一字符串与Checkbutton的值绑定,每次点击Checkbutton将打印出当前的值
v = StringVar()#声明v是tkinter里面的字符串变量
def callCheckbutton():
print(v.get()) Checkbutton(root,variable = v,text = 'checkbutton value',onvalue = 'python',offvalue='tkinter',command=callCheckbutton).pack()
root.mainloop()

Checkbutton的更多相关文章

  1. Tkinter教程之Checkbutton篇

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811306 #Tkinter教程之Checkbutton篇#Checkbutton又称为多选按 ...

  2. Checkbutton 和 Radiobutton

    The Checkbutton widget is used to display a number of options to a user as toggle buttons. The user ...

  3. Chapter 4. Button, Checkbutton, and Radiobutton Widgets 按钮,复选按钮,单选按钮

    Chapter 4. Button, Checkbutton, and Radiobutton Widgets   按钮,复选按钮,单选按钮 几乎所有的Perl/Tk 应用使用按钮以这样或者那样的方式 ...

  4. tkinter第三章(单选和多选)RadioButton CheckButton

    最简单的CheckButton多选类 import tkinter as tk #checkButton的内容,多选 root = tk.Tk() v = tk.IntVar()#装整形变量的 #va ...

  5. tkinter中checkbutton多选框控件和variable用法(六)

    checkbutton控件 简单的实现多选: import tkinter wuya = tkinter.Tk() wuya.title("wuya") wuya.geometry ...

  6. Python3 tkinter基础 Checkbutton variable 多选钮是否被选中

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. tkinter学习系列之(五)Checkbutton控件

    目录 目录 前言 (一)基本属性 (二)案例 1.简单的复选框 2.组合复选框 目录 前言 复选框:可以同时多选的一组框,其只有两种状态,选中与未选中. (一)基本属性 (1)说明: tkinter里 ...

  8. Python3 tkinter基础 Checkbutton anchor for生成多个控件并西对齐

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. DevExpress09、SimpleButton、CheckButton、DropDownButton、HScrollBar控件和VScrollBar控件

    SimpleButton控件 使用SimpleButton控件, 创建一个Button按钮, 可以通过其Image属性添加图片: 该控件与WinForm自带的Button按钮类似: 效果如下: Che ...

随机推荐

  1. Java I/O 总结

    Java I/O的的架构使用了装饰器的模式,我们在使用流的时候需要新建很多的装饰器对象,对源数据进行层层包装.各个包装类名以及它们的应用场景比较多,初学的时候难以摸清规律,这里我把它们归一下类,方便大 ...

  2. java-线程实现方式

    实现方式: 1,继承Thread类 public class ThreadTest extends Thread { @Override public void run() { System.out. ...

  3. PAT-L3-球队“食物链”-dfs-状压-set

    题目分析: 1. 一场双循环赛制的篮球赛,注意双循环,双循环! 2. 共有n只球队,两两之间有胜有负有平局: 3. 输入: 举例: 第一行:W:代表球队1打赢过这只队伍 L:代表球队2没打赢过这只队伍 ...

  4. 使用cocopod管理第三方

    扭捏了两年多一直不愿意使用cocopod来管理第三方,一直感觉直接拖拽第三方就挺方便的,直到今天使用第三方库WebViewJavascriptBridge,拖拽一直有问题,不得已研究.使用了cocop ...

  5. Git常用命令(一)------ 本地操作

    本文总结自廖雪峰的网站. 几个名词: 工作区(Working Directory):电脑里能看到的目录 版本库(Repository):包含暂存区和master 暂存区(Stage):待放入maste ...

  6. python实现K聚类算法

    参考:<机器学习实战>- Machine Learning in Action 一. 基本思想  聚类是一种无监督的学习,它将相似的对象归到同一簇中.它有点像全自动分类.聚类方法几乎可以应 ...

  7. Python 双向链表

    操作 is_empty() 链表是否为空 length() 链表长度 travel() 遍历链表 add(item) 链表头部添加 append(item) 链表尾部添加 insert(pos, it ...

  8. c/cpp语言链表连接部分详解

    核心代码: ①pTail->next = pNew; ②pNew->next = NULL; ③pTail = pNew; 设结构体名称为 struct ST: 注:方框代表分配的内存空间 ...

  9. scrapy 爬取当当网产品分类

    #spider部分import scrapy from Autopjt.items import AutopjtItem from scrapy.http import Request class A ...

  10. 记一次jar包冲突

    题记:永远不要在同一个项目中,引用不同版本的两个jar包,否则,这可能就是一个大坑. 在做网校项目的时候,帮助中心要使用lucene,所以就引入了lucene-5.5.1的包,删掉了原先存在于项目中的 ...