前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python GUI 03----But

Python GUI 03----But

作者头像
py3study
发布2020-01-08 18:06:36
6990
发布2020-01-08 18:06:36
举报
文章被收录于专栏:python3python3python3

1.一个简单的Button应用

from tkinter import *
#定义Button的回调函数
def helloButton():
    print ('hello button')
root = Tk()
#通过command属性来指定Button的回调函数
Button(root,text = 'Hello Button',command = helloButton).pack()
root.mainloop()

2.测试Button的relief属性

运行下面的代码可以看到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=RIDGE).pack()
Button(root,text = 'hello button',relief=SOLID).pack()
Button(root,text = 'hello button',relief=SUNKEN).pack()

root.mainloop()

3.与Label一样,Button也可以同时显示文本与图像,使用属性compound

from tkinter import *
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()

4.控件焦点问题

创建三个Button,各自对应回调函数;将第二个Button设置焦点,程序运行是按“Enter”,判断程序的打印结果

from tkinter import *

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)                            #Return事件相应回车点击。Enter事件响应的是mouseover
b3 = Button(root,text = 'Button3',command = cb3)
b1.pack()
b2.pack()
b3.pack()

b2.focus_set()                                     #将焦点定在按钮b2上
root.mainloop()

上例中使用了bind方法,它建立事件与回调函数(响应函数)之间的关系,每当产生<Enter>事件后,程序便自动的调用cb2,与cb1,cb3不同的是,它本身还带有一个参数----event,这个参数传递响应事件的信息。

5.指定Button的宽度与高度

width:    宽度 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()

7.改变Button的前景色与背景色

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 = str(b),
    bd = b).pack()
root.mainloop()    

9.设置Button状态

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属性

10.绑定Button与变量设置Button在textvariable属性

from tkinter import *
root = Tk()
def changeText():
    if b['text'] == 'text':
        v.set('change')
    else:
        v.set('text')
v = StringVar()
b = Button(root,textvariable = v,command = changeText)
v.set('text')
b.pack()
root.mainloop()

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.一个简单的Button应用
  • 2.测试Button的relief属性
  • 3.与Label一样,Button也可以同时显示文本与图像,使用属性compound
  • 4.控件焦点问题
  • 5.指定Button的宽度与高度
  • 6.设置Button文本在控件上的显示位置
  • 7.改变Button的前景色与背景色
  • 8.设置Button的边框bd(bordwidth):缺省为1或2个像素
  • 9.设置Button状态
  • 10.绑定Button与变量设置Button在textvariable属性
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档