Tkinter教程之Checkbutton篇

本文转载自:http://blog.csdn.net/jcodeer/article/details/1811306

#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 = ‘check 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.set(‘check python‘)
#绑定v到Checkbutton的属性textvariable
Checkbutton(root,text = ‘check python‘,textvariable = v,command = callCheckbutton).pack()

root.mainloop()

‘‘‘4.上述的textvariable使用方法与Button的用法完全相同,使用此例是为了区别Checkbutton的另外的一个属性variable,此属性与textvariable不同,它是与这个控件本身绑定,Checkbutton自己有值:On和Off值,缺省状态On为1,Off为0,如:‘‘‘
#显示Checkbutton的值
from Tkinter import *
root = Tk()
#将一整数与Checkbutton的值绑定,每次点击Checkbutton,将打印出当前的值
v = IntVar()
def callCheckbutton():
    print v.get()
Checkbutton(root,
            variable = v,
            text = ‘checkbutton value‘,
            command = callCheckbutton).pack()
root.mainloop()

‘‘‘5.Checkbutton的值不仅仅是1或0,可以是其他类型的数值,可以通过onvalue和offvalue属性设置Checkbutton的状态值,如下代码将On设置为‘python‘,Off值设置为‘Tkinter‘,程序的打印值将不再是0或1,而是‘Tkinter’或‘python’‘‘‘
from Tkinter import *
root = Tk()
#将一字符串与Checkbutton的值绑定,每次点击Checkbutton,将打印出当前的值
v = StringVar()
def callCheckbutton():
    print v.get()
Checkbutton(root,
            variable = v,
            text = ‘checkbutton value‘,
            onvalue = ‘python‘,        #设置On的值
            offvalue = ‘tkinter‘,    #设置Off的值
            command = callCheckbutton).pack()
root.mainloop()

# 6.还有其他的属性fg/bg/relief/width/height/justify/state使用方法与Button相同,不再举例。

时间: 2024-08-07 08:24:01

Tkinter教程之Checkbutton篇的相关文章

Tkinter教程之Radiobutton篇

本文转载自:http://blog.csdn.net/jcodeer/article/details/1811308 #Tkinter教程之Radiobutton篇#Radiobutton为单选按钮,即在同一组内只能有一个按钮被选中,每当选中组内的一个按钮时,其它的按钮自动改为非选中态,与其他控件不同的是:它有组的概念'''1.创建一个简单的Radiobutton'''from Tkinter import *root = Tk()Radiobutton(root,text = 'python'

Tkinter教程之Menu篇

本文转载自:http://blog.csdn.net/jcodeer/article/details/1811321 '''Tkinter教程之Menu篇''''''1.创建一个简单的Menu'''#添加菜单hello和quit,将hello菜单与hello函数绑定:quit菜单与root.quit绑定# -*- coding: cp936 -*-from Tkinter import *root = Tk()def hello():    print 'hello menu'menubar =

Tkinter教程之Listbox篇

本文转载自:http://blog.csdn.net/jcodeer/article/details/1811310 #Tkinter教程之Listbox篇#Listbox为列表框控件,它可以包含一个或多个文本项(text item),可以设置为单选或多选'''1.创建一个Listbox,向其中添加三个item'''from Tkinter import *root = Tk()lb = Listbox(root)for item in ['python','tkinter','widget']

Tkinter教程之Label篇

本文转载自:http://blog.csdn.net/jcodeer/article/details/1811293 #Tkinter教程之Label篇'''1.Label的第一个例子text属性使用方法'''#要使用Tk模块,除非你不想使用这个模块,那整个教程就不需要看了from Tkinter import *#初始化Tkroot = Tk()#创建一个label,使用编码,到现在为止还没有使用过直接通过“drag-and-drop”就可以完成的IDE.label = Label(root,

Tkinter教程之Entry篇

本文转载自:http://blog.csdn.net/jcodeer/article/details/1811302 #Tkinter教程之Entry篇#Entry用来输入单行文本'''1.第一个Entry程序'''from Tkinter import *root = Tk()Entry(root,text = 'input your text here').pack()root.mainloop()#上面的代码目的是创建一个Entry对象,并在Entry上显示'input your text

Tkinter教程之Scale篇

本文转载自:http://blog.csdn.net/jcodeer/article/details/1811313 '''Tkinter教程之Scale篇'''#Scale为输出限定范围的数字区间,可以为之指定最大值,最小值及步距值'''1.创建一个Scale'''from Tkinter import *root = Tk()Scale(root).pack()root.mainloop()#创建一个垂直Scale,最大值为100,最小值为0,步距值为1.这个参数设置也就是Scale的缺省设

Tkinter教程之Button篇(1)

本文转载自:http://blog.csdn.net/jcodeer/article/details/1811298 #Tkinter教程之Button篇(1)#Button功能触发事件'''1.一个简单的Button应用'''from Tkinter import *#定义Button的回调函数def helloButton():    print 'hello button'root = Tk()#通过command属性来指定Button的回调函数Button(root,text = 'He

Tkinter教程之Button篇(2)

本文转载自:http://blog.csdn.net/jcodeer/article/details/1811300 # Tkinter教程之Button篇(2)'''5.指定Button的宽度与高度width:    宽度heigth:    高度使用三种方式:1.创建Button对象时,指定宽度与高度2.使用属性width和height来指定宽度与高度3.使用configure方法来指定宽度与高度'''from Tkinter import *root = Tk()b1 = Button(r

Tkinter教程之Message篇

本文转载自:http://blog.csdn.net/jcodeer/article/details/1811326 '''Tkinter教程之Message篇'''#Message也是用来显示文本的,用法与Label基本一样'''1.创建一个简单的Message'''from Tkinter import *root = Tk()Message(root,text = 'hello Message').pack()root.mainloop()#运行程序,可以看到Hello之后,Message