python tkinter button_Python3 Tkinter-Button

1.绑定事件处理函数

from tkinter import *

def hello():

print('Hello!')

root=Tk()

button=Button(root,text='click me!',command=hello)

button.pack()

root.mainloop()

dc764cec9fa9

图片.png

2.按钮样式

from tkinter import *

def hello():

print('Hello!')

root=Tk()

button1=Button(root,text='click me!',command=hello,relief=FLAT)

button1.pack()

button2=Button(root,text='click me!',command=hello,relief=GROOVE)

button2.pack()

button3=Button(root,text='click me!',command=hello,relief=RAISED)

button3.pack()

button4=Button(root,text='click me!',command=hello,relief=RIDGE)

button4.pack()

button5=Button(root,text='click me!',command=hello,relief=SOLID)

button5.pack()

button6=Button(root,text='click me!',command=hello,relief=SUNKEN)

button6.pack()

root.mainloop()

dc764cec9fa9

图片.png

3.图像

button也有bitmap,compound

4.焦点

from tkinter import *

def hello():

print('Hello!')

def b2(event):

print(event,' is clicked.')

root=Tk()

button1=Button(root,text='click me!',command=hello)

button1.pack()

button2=Button(root,text='click me!')

button2.bind('',b2)

button2.pack()

button2.focus_set()

button1.focus_set()

root.mainloop()

dc764cec9fa9

图片.png

5.宽高

b.configure=(width=30,heigth=100)

也可在定义的时候确定

6.Button文本在控件上显示的位置

from tkinter import *

def hello():

print('Hello!')

root=Tk()

button1=Button(root,text='click me!',command=hello,anchor='ne',width=30,height=4)

button1.pack()

root.mainloop()

dc764cec9fa9

图片.png

n(north),s(south),w(west),e(east),ne(north east),nw,se,sw

7.改变颜色

from tkinter import *

def hello():

print('Hello!')

root=Tk()

button1=Button(root,text='click me!',command=hello,fg='red',bg='blue')

button1.pack()

root.mainloop()

dc764cec9fa9

图片.png

8.边框

from tkinter import *

def hello():

print('Hello!')

def b2(event):

print(event,' is clicked.')

root=Tk()

for b in [0,1,2,3,4]:

Button(root,text=str(b),bd=b).pack()

root.mainloop()

dc764cec9fa9

图片.png

9.状态

from tkinter import *

def hello():

print('Hello!')

def b2(event):

print(event,' is clicked.')

root=Tk()

for r in ['norma','active','disabled']:

Button(root,state=r,text=r).pack()

root.mainloop()

dc764cec9fa9

图片.png

10.绑定变量

from tkinter import *

root=Tk()

def change():

if b['text']=='text':

v.set('change')

else:

v.set('text')

v=StringVar()

b=Button(root,textvariable=v,command=change)

v.set('text')

b.pack()

root.mainloop()

dc764cec9fa9

图片.png

/w/1240)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值