tkinker下Button控件

Tkinker

导入库

from tkinter import * 

一个简单的Button应用

root = Tk()
# 定义Button的回调函数
def HW():
    print('Hello World!')
# 通过command属性来指定Button的回调函数
Button(root, text='HB', command=HW).pack()
root.mainloop()

Button的relief属性

# flat, groove, raised, ridge, solid, or sunken
root = Tk()
for r in ['flat','groove','raised','ridge','solid','sunken']:
    Button(root,text = r,relief = r,width = 30).pack()
root.mainloop()

使用属性compound显示文本与图像

root = Tk()
# 图像居下,居上,居右,居左,文字位于图像之上
for tc in ['bottom','top','left','center','right']:
    Button(root, text=tc, compound=tc, bitmap='error').pack()
# 消息循环
root.mainloop()

控件焦点问题

# 创建Button,各自对应回调函数;将第二个Button设置焦点,程序运行是按“Enter”,判断程序的打印结果
from tkinter import *
def printEventInfo(event):
    print('event.time = ', event.time)
    print('event.type = ', event.type)
    print('event.WidgetId = ', event.widget)
    print('event.KeySymbol = ', event.keysym) 

root = Tk() 
B = Button(root, text='Button',width = 40)
B.bind("<Enter>", printEventInfo)
B.pack()
 
B.focus_set()
root.mainloop()

指定Button的宽度与高度

"""
width:    宽度
heigth:    高度
使用三种方式:
1.创建Button对象时,指定宽度与高度
2.使用属性width和height来指定宽度与高度
3.使用configure方法来指定宽度与高度
"""
root = Tk()
b1 = Button(root, text='30X1', width=80, height=2)
b1.pack() 
# b2['width'] = 30 b2['height'] = 3
# b3.configure(width=30, height=3) 
root.mainloop()

设置Button文本在控件上的显示位置

"""
anchor:使用的值为:n(north),s(south),w(west),e(east)和ne,nw,se,sw,就是地图上的标识位置了,
        使用width和height属性是为了显示各个属性的不同。
"""
root = Tk() 
for a in ['center','n', 's', 'e', 'w', 'ne', 'nw', 'se', 'sw']:
    Button(root,text='anchor',anchor=a,width=30,height=4).pack()
root.mainloop()

Button的前景色与背景色

root = Tk()
bfg = Button(root, text='ChangeFore&Broundground', fg='red',bg='blue')
bfg.pack()
root.mainloop()

设置Button的边框

# bd(bordwidth):缺省为1或2个像素
# 创建5个Button边框宽度依次为:0,2,4,6,8
root = Tk()
for b in [0, 1, 2, 3, 4]:
    Button(root,text=str(b),bd=b).pack()
root.mainloop()

设置Button状态

root = Tk()
 
# normal/active/disabled
# 三个Button在回调函数都设置为statePrint,运行程序只有normal和active激活了回调函数,
# 而disable按钮则没有,对于暂时不需要按钮起作用时,可以将它的state设置为disabled属性
def statePrint():
    print('state')
for r in ['normal','active','disabled']:
    Button(root,text = r,state = r,width = 30,command = statePrint).pack() 
root.mainloop()

绑定Button与变量

# 设置Button在textvariable属性
# 将变量v与Button绑定,当v值变化时,Button显示的文本也随之变化
root = Tk()

def changeText():
    if b['text'] == 'text':
        v.set('change')
        print('change')
    else:
        v.set('text')
        print('text') 

v = StringVar()
b = Button(root, textvariable=v, command=changeText)
v.set('text')
b.pack()
root.mainloop()

Button参数大餐

        """Construct a button widget with the parent MASTER.
           用父控件构造一个按钮小部件。
        STANDARD OPTIONS  标准选项

            activebackground:激活背景
            activeforeground:激活前景
            anchor:按钮上文字位置
            background:背景
            bitmap:位现图
            borderwidth:边界宽度
            cursor:鼠标
            disabledforeground:注销前景
            font:字体状态
            foreground:前景
            highlightbackground:高亮背景
            highlightcolor:高亮颜色
            highlightthickness:高亮厚度
            image:图片
            justify:对齐方式
            padx:x填补宽度
            pady:y填补高度
            relief:按钮浮动显示方式
            repeatdelay:重复延迟
            repeatinterval:重复间距
            takefocus:聚焦
            text:按钮名称
            textvariable:文本变量
            underline:下划线
            wraplength:外围长度

        WIDGET-SPECIFIC OPTIONS   特定的选项

            command:响应函数
            compound:按钮上图标悬浮方式
            default:默认
            height:按钮高度
            overrelief:还未知,望读者留言告知
            state:按钮激活状态
            width:按钮宽度
        """

注:转发请注明原址,谢谢配合!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值