Python Tkinter Button按钮


Python Tkinter Button按钮

  1. BUTTON 小工具时使用的按钮添加到各种类型的Python应用。Python允许用户配置按钮的按我们的要求。各种选项可以被设置或重置的要求。

  2. 我们还可以将方法或功能的按钮,当按钮被按压

  3. 语法

button = Button(parent,options)
  1. 可能选项的列表
选项 描述
activebackground 它表示背景的按钮在按钮上悬停鼠标的动作,改变按钮背景色
activeforeground 它表示字体颜色的按钮,当鼠标悬停在按钮上改变前景色
bd 它表示按钮的边界宽度
command 它被设置为回调函数时,可以调用该函数
bg 设置按钮背景的颜色
b 设置按钮的前景颜色
font 设置按钮文本的字体属性
height 设置按钮的高度
width 按钮的宽度,它以文本按钮的字母数或图像按钮的像素数的形式存在
image 设置图像上显示的按钮
padx 按钮在水平方向上的附加填充
pady 按钮在垂直方向上的附加填充
justify 设置多个文本行为左对齐、右对齐或者居中对齐
highlightcolor 设置当按钮具有焦点突出显示的颜色
relief 它表示边框的类型,可以设置SUNKEN, RAISED, GROOVE, and RIDGE
state 将此选项设置为禁用,以使按钮无响应。活动表示按钮的活动状态
underline 设定此选项以使按钮文本下划线
wraplength 如果该值被设置为正数,文本行将被包装成适合这个长度
  1. 示例一
from tkinter import *   
root = Tk()  
root.geometry("200x100")  
b = Button(root, text = "Simple")  
b.pack()  
root.mainaloop()
  • 输出

  1. 示例二
from tkinter import *   
root = Tk()  
root.geometry("200x100")  
def fun():  
    messagebox.showinfo("Hello", "Red Button clicked")
  
b1 = Button(root,text = "Red",command = fun,activeforeground = "red",activebackground = "pink",pady=10)  
b2 = Button(root, text = "Blue",activeforeground = "blue",activebackground = "pink",pady=10)  
b3 = Button(root, text = "Green",activeforeground = "green",activebackground = "pink",pady = 10)  
b4 = Button(root, text = "Yellow",activeforeground = "yellow",activebackground = "pink",pady = 10)  
b1.pack(side = LEFT)  
b2.pack(side = RIGHT)  
b3.pack(side = TOP)  
b4.pack(side = BOTTOM)  
root.mainloop()
  • 输出


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM