Python graphics editor Tkinter Button (2)

#coding=utf-8

'''5.Button 的宽度与高度'''
# width: 宽度
# heigth: 高度
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()

# 使用三种方式:
# 1.创建 Button 对象时,指定宽度与高度
# 2.使用属性 width 和 height 来指定宽度与高度 
#  3.使用 configure 方法来指定宽度与高度


'''6.设置 Button 文本在控件上的显示位置'''
#anchor:指定组件的位置,可用的值为:n(north),s(south),w(west),e(east)和 ne,nw,se,sw, 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()
# 使用 width 和 height 属性是为了显示各个属性的不同。
'''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:指定外观效果 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 状态'''
# state:指定组件状态:正常(norma)/(激活)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 与变量'''
# textvariable:设置 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 显示的文本也随之变化

Guess you like

Origin blog.csdn.net/clarence20170301/article/details/76624004