Tkinter Checkbutton

Python - Tkinter Checkbutton: checkbutton小部件用于显示切换按钮的用户多项选择。然后,用户可以通过点击相应的按钮每个选项中选择一个或多个选项.
 
checkbutton小部件用于显示切换按钮的用户多项选择。然后,用户可以通过点击相应的按钮每个选项中选择一个或多个选项.

您还可以显示在图像代替文字.

语法:

这里是一个简单的语法来创建这个widget:

w = Checkbutton ( master, option, ... )

参数:

  • master: 这表示父窗口.

  • options: 下面是这个小工具最常用的选项列表。这些选项可以作为键 - 值对以逗号分隔.

OptionDescription
activebackgroundBackground color when the checkbutton is under the cursor.
activeforegroundForeground color when the checkbutton is under the cursor.
bgThe normal background color displayed behind the label and indicator.
bitmapTo display a monochrome image on a button.
bdThe size of the border around the indicator. Default is 2 pixels.
commandA procedure to be called every time the user changes the state of this checkbutton.
cursorIf you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the checkbutton.
disabledforegroundThe foreground color used to render the text of a disabled checkbutton. The default is a stippled version of the default foreground color.
fontThe font used for the text.
fgThe color used to render the text.
heightThe number of lines of text on the checkbutton. Default is 1.
highlightcolorThe color of the focus highlight when the checkbutton has the focus.
imageTo display a graphic image on the button.
justifyIf the text contains multiple lines, this option controls how the text is justified: CENTER, LEFT, or RIGHT.
offvalueNormally, a checkbutton's associated control variable will be set to 0 when it is cleared (off). You can supply an alternate value for the off state by setting offvalue to that value.
onvalueNormally, a checkbutton's associated control variable will be set to 1 when it is set (on). You can supply an alternate value for the on state by setting onvalue to that value.
padxHow much space to leave to the left and right of the checkbutton and text. Default is 1 pixel.
padyHow much space to leave above and below the checkbutton and text. Default is 1 pixel.
reliefWith the default value, relief=FLAT, the checkbutton does not stand out from its background. You may set this option to any of the other styles
selectcolorThe color of the checkbutton when it is set. Default is selectcolor="red".
selectimageIf you set this option to an image, that image will appear in the checkbutton when it is set.
stateThe default is state=NORMAL, but you can use state=DISABLED to gray out the control and make it unresponsive. If the cursor is currently over the checkbutton, the state is ACTIVE.
textThe label displayed next to the checkbutton. Use newlines ("\n") to display multiple lines of text.
underlineWith the default value of -1, none of the characters of the text label are underlined. Set this option to the index of a character in the text (counting from zero) to underline that character.
variableThe control variable that tracks the current state of the checkbutton. Normally this variable is an IntVar, and 0 means cleared and 1 means set, but see the offvalue and onvalue options above.
widthcheckbutton的默认宽度取决于所显示的图像或文字的大小。您可以设置此选项的字符数和checkbutton的,总是有许多字符的空间.
wraplengthNormally, lines are not wrapped. You can set this option to a number of characters and all lines will be broken into pieces no longer than that number.

方法:

以下是这个小工具的常用方法:

MedthodDescription
deselect()Clears (turns off) the checkbutton.
flash()Flashes the checkbutton a few times between its active and normal colors, but leaves it the way it started.
invoke()You can call this method to get the same actions that would occur if the user clicked on the checkbutton to change its state.
select()Sets (turns on) the checkbutton.
toggle()Clears the checkbutton if set, sets it if cleared.

例子:

自行尝试下面的例子:

from Tkinter import *
import tkMessageBox
import Tkinter

top = Tkinter.Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
C1 = Checkbutton(top, text = "Music", variable = CheckVar1, \
                 onvalue = 1, offvalue = 0, height=5, \
                 width = 20)
C2 = Checkbutton(top, text = "Video", variable = CheckVar2, \
                 onvalue = 1, offvalue = 0, height=5, \
                 width = 20)
C1.pack()
C2.pack()
top.mainloop()

这将产生以下结果:

 

转载于:https://www.cnblogs.com/tkinter/p/5628887.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值