zoukankan      html  css  js  c++  java
  • Tkinter教程之Button篇(2)

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

    # Tkinter教程之Button篇(2)

    '''5.指定Button的宽度与高度
        宽度
    heigth:    高度
    使用三种方式:
    1.创建Button对象时,指定宽度与高度
    2.使用属性width和height来指定宽度与高度
    3.使用configure方法来指定宽度与高度
    '''
    from Tkinter import *
    root = Tk()
    b1 = Button(root,text = '30X1',width = 30,height = 2)
    b1.pack()

    b2 = Button(root,text = '30X2')
    b2['width'] = 30
    b2['height'] = 3
    b2.pack()

    b3 = Button(root,text = '30X3')
    b3.configure(width = 30,height = 3)
    b3.pack()

    root.mainloop()
    # 上述的三种方法同样也适合其他的控件
    '''6.设置Button文本在控件上的显示位置
    anchor:
    使用的值为:n(north),s(south),w(west),e(east)和ne,nw,se,sw,就是地图上的标识位置了,使用
    width和height属性是为了显示各个属性的不同。
    '''
    from Tkinter import *
    root = Tk()

    #简单就是美!
    for a in ['n','s','e','w','ne','nw','se','sw']:
        Button(root,
        text = 'anchor',
        anchor = a,
        width = 30,
        height = 4).pack()
    #如果看的不习惯,就使用下面的代码。
    # Button(root,text = 'anchor',width = 30,height =4).pack()
    # Button(root,text = 'anchor',anchor = 'center',width = 30,height =4).pack()
    # Button(root,text = 'anchor',anchor = 'n',width = 30,height = 4).pack()
    # Button(root,text = 'anchor',anchor = 's',width = 30,height = 4).pack()
    # Button(root,text = 'anchor',anchor = 'e',width = 30,height = 4).pack()
    # Button(root,text = 'anchor',anchor = 'w',width = 30,height = 4).pack()
    # Button(root,text = 'anchor',anchor = 'ne',width = 30,height = 4).pack()
    # Button(root,text = 'anchor',anchor = 'nw',width = 30,height = 4).pack()
    # Button(root,text = 'anchor',anchor = 'se',width = 30,height = 4).pack()
    # Button(root,text = 'anchor',anchor = 'sw',width = 30,height = 4).pack()

    root.mainloop()
    '''7.改变Button的前景色与背景色
    fg:    前景色
    bg:背景色
    '''
    from Tkinter import *
    root = Tk()
    bfg = Button(root,text = 'change foreground',fg = 'red')
    bfg.pack()

    bbg = Button(root,text = 'change backgroud',bg = 'blue')
    bbg.pack()

    root.mainloop()

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

    '''9.设置Button的风格
    relief/raised/sunken/groove/ridge
    '''
    from Tkinter import *
    root = Tk()
    for r in ['raised','sunken','groove','ridge']:
        Button(root,
        text = r,
        relief = r,
        width = 30).pack()
    root.mainloop()

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

    '''11.绑定Button与变量
    设置Button在textvariable属性
    '''
    from Tkinter import *
    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()

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

  • 相关阅读:
    正则表达式(四)--文本换行分割
    java加密类型和算法名称
    记事本记录日志
    DNS
    jstl--c:choose标签
    csv文本编辑引号问题
    JDBC----ReflectionUtils
    Jsp
    计算机网络 编程 总结:
    N颗骰子的问题
  • 原文地址:https://www.cnblogs.com/LeeZz/p/3984298.html
Copyright © 2011-2022 走看看