Python tkinter之Button
1、Button的基本属性
# -*- encoding=utf-8 -*- import tkinter from tkinter import * def event(): print('点击事件') if __name__ == '__main__': win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 500 height = 300 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 button = Button( master=win, # 父容器 text='标签', # 文本 bg='yellow', # 背景颜色 fg='red', # 文本颜色 activebackground='pink', # 状态为active时的背景颜色 activeforeground='blue', # 状态为active的文字颜色 relief='raised', # 边框的3D样式 flat、sunken、raised、groove、ridge、solid。默认为 raised。 bd=3, # 边框的大小 height=1, # 高度 width=5, # 宽度 padx=1, # 内间距,字体与边框的X距离 pady=1, # 内间距,字体与边框的Y距离 state='normal', # 设置状态 normal、active、 disabled 默认 normal cursor='arrow', # 鼠标移动时样式 arrow, circle, cross, plus... font=('黑体', 20), # 字体 command=event, # 点击事件 ) button.pack() win.mainloop()
备注:
①如果同时设置了width、height和padx、pady,最后确定大小是根据谁值大选谁
②支持的字体(通过tkinter.font.families获取)https://www.cnblogs.com/rainbow-tan/p/14043822.html/
③鼠标样式选项
values = ["arrow", "circle", "clock", "cross", "dotbox", "exchange", "fleur", "heart", "man", "mouse", "pirate", "plus",
"shuttle", "sizing", "spider", "spraycan", "star","target", "tcross", "trek", "watch"]
2、边框样式,组件状态阅览
import tkinter from tkinter import * def event(): print('点击事件') if __name__ == '__main__': win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 980 height = 300 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 values = ['flat', 'sunken', 'raised', 'groove', 'ridge', 'solid'] for index, value in enumerate(values): button = Button( master=win, # 父容器 text=value, # 文本 bg='yellow', # 背景颜色 fg='red', # 文本颜色 activebackground='pink', # 状态为active时的背景颜色 activeforeground='blue', # 状态为active的文字颜色 relief=value, # 边框的3D样式 flat、sunken、raised、groove、ridge、solid。 bd=5, # 边框的大小 height=1, # 高度 width=10, # 宽度 padx=1, # 内间距,字体与边框的X距离 pady=1, # 内间距,字体与边框的Y距离 state='normal', # 设置状态 normal、active、 disabled 默认 normal cursor='arrow', # 鼠标移动时样式 arrow, circle, cross, plus... font=('Yu Gothic Medium', 15), # 字体 command=event, # 点击事件 ) button.grid(row=0, column=index, padx=10, pady=10) values = ['normal', 'active', 'disabled'] for index, value in enumerate(values): button = Button( master=win, # 父容器 text=value, # 文本 bg='yellow', # 背景颜色 fg='red', # 文本颜色 activebackground='pink', # 状态为active时的背景颜色 activeforeground='blue', # 状态为active的文字颜色 relief='raised', # 边框的3D样式 flat、sunken、raised、groove、ridge、solid。 bd=3, # 边框的大小 height=1, # 高度 width=10, # 宽度 padx=1, # 内间距,字体与边框的X距离 pady=1, # 内间距,字体与边框的Y距离 state=value, # 设置状态 normal、active、 disabled 默认 normal cursor='arrow', # 鼠标移动时样式 arrow, circle, cross, plus... font=('Yu Gothic Medium', 15), # 字体 command=event, # 点击事件 ) button.grid(row=1, column=index, padx=10, pady=10) win.mainloop()
3、显示图片的Button
tkinter只支持gif图片,如果使用其他格式图片,需要使用PIL模块
# -*- encoding=utf-8 -*- import tkinter from tkinter import * from PIL import Image from PIL import ImageTk def event(): print('点击事件') if __name__ == '__main__': win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 500 height = 300 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 img_open = Image.open('../img/19.png') img_png = ImageTk.PhotoImage(img_open) button = Button( master=win, # 父容器 text='标签', # 文本 bg='pink', # 背景颜色 fg='red', # 文本颜色 activebackground='pink', # 状态为active时的背景颜色 activeforeground='blue', # 状态为active的文字颜色 relief='groove', # 边框的3D样式 flat、sunken、raised、groove、ridge、solid。 bd=3, # 边框的大小 height=64, # 高度 width=64, # 宽度 padx=1, # 内间距,字体与边框的X距离 pady=1, # 内间距,字体与边框的Y距离 state='normal', # 设置状态 normal、active、 disabled cursor='arrow', # 鼠标移动时样式 arrow, circle, cross, plus... font=('黑体', 20), # 字体 image=img_png, # 图片 command=event, # 点击事件 ) button.pack() win.mainloop()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏