您的位置:首页 > 编程语言 > Python开发

Python 图形Tkinter Button

2017-08-03 15:12 357 查看
#coding=utf-8
'''第一个button 例子'''
#command:指定时间处理函数
from Tkinter import *
#定义Button的时间处理函数
def helloButton():
print 'hello Button'

root = Tk()
#通过 command 属性来指定Button的事件处理函数
Button(root,
text='点击一次控制台打印一句 hello Button',
command=helloButton
).pack()
root.mainloop()

#coding=utf-8
'''Button 的外观效果'''

from Tkinter import *
root = Tk()
#flat, groove, raised, ridge, solid, or sunken
Button(root,
text='hello button',
relief=FLAT
).pack()
Button(root,
text='hello button',
relief=GROOVE
).pack()

Button(root,
text='hello button',
relief=RAISED
).pack()
Button(root,
text='hello button',
relief=SOLID
).pack()
Button(root,
text='hello button',
relief=SUNKEN
).pack()

#root.mainloop()

#图像居下,居上,居右,居左,文字位于图像之上
#root = Tk()
Button(root,
text = 'botton',
compound = 'bottom',
bitmap = 'error'
).pack()
Button(root,
text = 'top',
compound = 'top',
bitmap = 'error'
).pack()
Button(root,
text = 'right',
compound = 'right',
bitmap = 'error'
).pack()
Button(root,
text = 'left',
compound = 'left',
bitmap = 'error'
).pack()
Button(root,
text = 'center',
compound = 'center',
bitmap = 'error'
).pack()

#root.mainloop()

'''Button 的焦点'''
# focus_set:设置当前组件得到焦点
#创建三个 Button,各自对应回调函数;
# 将第二个 Button 设置焦点,程序运行是按“Enter”,
#  判断程序的打印结果

def cb1():
print 'button1 clicked'
def cb2(event):
print 'button2 clicked'
def cb3():
print 'button3 clicked'

#root = Tk()

b1 = Button(root,text = 'Button1',command = cb1)
b2 = Button(root,text = 'Button2')
b2.bind("<Return>",cb2)
b3 = Button(root,text = 'Button3',command = cb3)
b1.pack()
b2.pack()
b3.pack()
b2.focus_set()

root.mainloop()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python
相关文章推荐