tkinter -- button2

发布时间:2018-05-31 21:45:24编辑:Run阅读(2911)

    指定button的宽度和高度

    width:  宽度

    height: 高度


    使用三种方式:

    1 创建button对象时,指定宽度与高度

    2 使用属性width和height来指定宽度和高度

    3 使用configure方法来指定宽度与高度


    示例:

    import tkinter as tk
    
    root = tk.Tk()
    # 创建button对象时,指定宽度与高度
    b1 = tk.Button(root, text='A1', width=30, height=2)
    b1.pack()
    
    # 使用属性width和height来指定宽度和高度
    b2 = tk.Button(root, text='B1')
    b2['width'] = 30
    b2['height'] = 3
    b2.pack()
    
    # 使用configure方法来指定宽度与高度
    b3 = tk.Button(root, text='C3')
    b3.configure(width=30, height=3)
    b3.pack()
    
    # 进入消息循环
    root.mainloop()

    效果:

    blob.png



    设置Button文本在控件上的显示位置,就是地图上的标识位置了

    width和height属性是为了显示各个属性的不同

    使用的值:

    n(north)  北

    s(south)  南

    w(west)   西

    e(east)   东

    ne(north-east) 东北

    nw(north-west) 西北

    se(south-east) 东南

    sw(south-west) 西南


    示例代码:

    方法1:推荐使用

    import tkinter as tk
    
    root = tk.Tk()
    
    for i in ['n', 's', 'e', 'w', 'ne', 'nw', 'se', 'sw']:
        tk.Button(root, text='py3study', anchor=i, width=30, height=3).pack()
    
    
    # 进入消息循环
    root.mainloop()

    效果:

    blob.png



    方法2:效果一样,但是代码重复,不推荐使用

    import tkinter as tk
    
    root = tk.Tk()
    
    tk.Button(root, text='py3study', width=30, height=4).pack()
    tk.Button(root, text='py3study', anchor='center', width=30, height=4).pack()
    tk.Button(root, text='py3study', anchor='n', width=30, height=3).pack()
    tk.Button(root, text='py3study', anchor='s', width=30, height=3).pack()
    tk.Button(root, text='py3study', anchor='e', width=30, height=3).pack()
    tk.Button(root, text='py3study', anchor='w', width=30, height=3).pack()
    tk.Button(root, text='py3study', anchor='ne', width=30, height=3).pack()
    tk.Button(root, text='py3study', anchor='nw', width=30, height=3).pack()
    tk.Button(root, text='py3study', anchor='se', width=30, height=3).pack()
    tk.Button(root, text='py3study', anchor='sw', width=30, height=3).pack()
    
    
    # 进入消息循环
    root.mainloop()

    效果:

    blob.png


    改变button的前景色与背景色

    fg: 前景色

    bg: 背景色


    示例:

    import tkinter as tk
    root = tk.Tk()
    bfg = tk.Button(root, text='py3study', fg='red')
    bbg = tk.Button(root, text='py3study', bg='blue')
    
    bfg.pack()  # 显示button
    bbg.pack()  # 显示button
    
    root.mainloop()  # 进入消息循环

    效果:

    blob.png



    设置Button的边框

    bd(bordwidth): 边框宽度

    创建5个 Button 边框宽度依次为:0,2,4,6,8


    示例:

    import tkinter as tk
    root = tk.Tk()
    
    for b in range(10):  # 取边框长度为0,2,4,6,8
        if b % 2 == 0:
            tk.Button(root, text=str(b), bd=b).pack()
    
    root.mainloop()

    效果:

    blob.png



    设置button的风格,relief浮雕效果

    raised  凸起

    sunken  凹陷

    groove  沟

    ridge   脊


    示例:

    import tkinter as tk
    root = tk.Tk()
    for i in ['raised', 'sunken', 'groove', 'ridge']:
        tk.Button(root, text=i, relief=i, width=30).pack()
    
    root.mainloop()

    效果:

    blob.png



    设置Button状态 -- 重点

    normal   正常

    active   活动

    disabled 禁用


    示例:

    import tkinter as tk
    root = tk.Tk()
    
    def normalprint():
        print('state {}'.format('normal'))
    
    def activeprint():
        print('state {}'.format('active'))
    
    def disabled():
        print('sate {}'.format('disabled'))
    
    # command调用函数 command=函数名
    for i in ('normal', 'active', 'disabled'):
        if i == 'normal':
            tk.Button(root, text=i, state=i, width=30, command=normalprint).pack()
        elif i == 'active':
            tk.Button(root, text=i, state=i, width=30, command=activeprint).pack()
        else:
            tk.Button(root, text=i, state=i, width=30, command=disabled).pack()
    
    root.mainloop()

    效果:

    blob.png



    绑定Button与变量 -- 重点

    设置button在textvariable(文本变量)属性

    StringVar是Tk库内部定义的字符串变量类型,在这里用于管理部件上面的字符;不过一般用在按钮button上

    示例:

    import tkinter as tk
    root = tk.Tk()
    
    def changtext():
        if b['text'] == 'text':
            v.set('change')
            print('change')
        else:
            v.set('text')
            print('text')
    
    v = tk.StringVar()
    b = tk.Button(root, textvariable=v, command=changtext)
    v.set('text')
    b.pack()
    root.mainloop()

    效果:

    444.gif

    将变量 v 与 Button 绑定,当 v 值变化时,Button 显示的文本也随之变化

关键字

上一篇: tkinter -- button1

下一篇: tkinter -- Entry