BUTTON 小工具时使用的按钮添加到各种类型的Python应用。Python允许用户配置按钮的按我们的要求。各种选项可以被设置或重置的要求。
我们还可以将方法或功能的按钮,当按钮被按压。
按钮小工具的使用语法如下:
语法
W = Button(parent,options)
可能选项的列表如下。
Sn |
选项 |
描述 |
1 | activebackground |
它表示背景的按钮在按钮上悬停鼠标的动作,改变按钮背景色 |
2 | activeforeground |
它表示字体颜色的按钮,当鼠标悬停在按钮上改变前景色 |
3 | bd |
它表示按钮的边界宽度 |
4 | bg |
设置按钮背景的颜色 |
5 | Command |
它被设置为回调函数时,可以调用该函数 |
6 | fg |
设置按钮的前景颜色 |
7 | Font |
设置按钮文本的字体属性 |
8 | Height |
设置按钮的高度 |
9 | Highlightcolor |
设置当按钮具有焦点突出显示的颜色 |
10 | Image |
设置图像上显示的按钮 |
11 | justify |
设置多个文本行为左对齐、右对齐或者居中对齐 |
12 | Padx | 按钮在水平方向上的附加填充 |
13 | pady |
按钮在垂直方向上的附加填充 |
14 | Relief |
它表示边框的类型,可以设置SUNKEN, RAISED, GROOVE, and RIDGE |
15 | State |
将此选项设置为禁用,以使按钮无响应。活动表示按钮的活动状态 |
16 | Underline |
设定此选项以使按钮文本下划线 |
17 | Width |
按钮的宽度,它以文本按钮的字母数或图像按钮的像素数的形式存在 |
18 | Wraplength |
如果该值被设置为正数,文本行将被包装成适合这个长度 |
实例
#python application to create a simple button from tkinter import * top = Tk() top.geometry("200x100") b = Button(top,text = "Simple") b.pack() top.mainaloop()
输出 :
实例
from tkinter import * top = Tk() top.geometry("200x100") def fun(): messagebox.showinfo("Hello", "Red Button clicked") b1 = Button(top,text = "Red",command = fun,activeforeground = "red",activebackground = "pink",pady=10) b2 = Button(top, text = "Blue",activeforeground = "blue",activebackground = "pink",pady=10) b3 = Button(top, text = "Green",activeforeground = "green",activebackground = "pink",pady = 10) b4 = Button(top, text = "Yellow",activeforeground = "yellow",activebackground = "pink",pady = 10) b1.pack(side = LEFT) b2.pack(side = RIGHT) b3.pack(side = TOP) b4.pack(side = BOTTOM) top.mainloop()
输出: