Pythontkinter之Button

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Pythontkinter之Button
1、Button的基本属性
# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *
def event():
print('点击事件')
if__name__ == '__main__':
win = () # 窗⼝
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,最后确定⼤⼩是根据谁值⼤选谁
③⿏标样式选项
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 = () # 窗⼝
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 = () # 窗⼝
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()。

相关文档
最新文档