十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
按钮的使用方法Button(父对象,options......)
options的取值borderwidth或bd:边界宽度默认是两个像素;bg:背景颜色;command:点击按钮时,执行此方法;cursor:鼠标移动到按钮上的形状;fg:前景色。font:字形;height:高;highlightbackground:按钮获得焦点时的背景颜色;highlightcolor:按钮取得焦点时的颜色;image:按钮上的图像;justify:当有多行文字时,最后一行文字的对齐方式;padx/pady:默认是1,可以设置按钮与文字的间隔以及按钮的上下间距;relief:默认是relief=FLAT,可以控制文字外框;state:默认是state=NORMAL,如果设置为DISABLED则以灰阶显示功能按钮,表示暂时无法使用;text:按钮的名称;underline:设置第几个文字有下划线,从0开始算起,默认-1是无下划线;width:宽,单位是字符宽;wraplength:限制每行的文字数,默认是0,表示只有\n才会换行。
创新互联建站专注于寿宁企业网站建设,响应式网站,商城系统网站开发。寿宁网站建设公司,为寿宁等地区提供建站服务。全流程定制设计,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务
32-设置一个按钮,当点击该按钮时,可以显示字符串“I LIKE TKINTER”,背景颜色是蓝色,字符串的颜色是白色。
from tkinter import *
win=Tk()
win.geometry('400x500')
def show():
label['text']="I LIKE TKINTER"
label['bg']="blue"
label['fg']="white"
label=Label(win)
button=Button(win,text="点我打印消息",command=show)
label.pack()
button.pack()
win.mainloop()
33-设置一个点击取消可以关闭的窗口。
from tkinter import *
win=Tk()
def show():
label.config(text="I LIKE TKINTER",bg="blue",fg="white")
label=Label(win)
label.pack()
button1=Button(win,text="打印消息",command=show)
button2=Button(win,text="取消",command=win.destroy)
button1.pack(side=LEFT)
button2.pack(side=LEFT)
win.mainloop()
34-设计一个计时器程序,添加一个结束按钮,当点击结束按钮时,程序执行结束。
from tkinter import *
win=Tk()
counter=0
def run_counter(digit):
def counting():
global counter
counter+=1
digit.config(text=str(counter))
digit.after(1000,counting)
counting()
digit=Label(win,bg="yellow",fg="black",height=3,width=10,font="宋体 20 bold")
digit.pack()
run_counter(digit)
Button(win,text="结束",width=15,command=win.destroy).pack()
win.mainloop()
35-设置三个按钮点击blue按钮可以将窗口背景颜色设置为蓝色,点击red按钮可以将窗口颜色设置为红色,点击exit按钮退出程序。
from tkinter import *
win=Tk()
def red():
win.config(bg="red")
def blue():
win.config(bg="blue")
redbutton=Button(win,text="red",command=red)
bluebutton=Button(win,text="blue",command=blue)
exitbutton=Button(win,text="exit",command=win.destroy)
redbutton.pack(anchor=S,side=RIGHT,padx=5,pady=5)
bluebutton.pack(anchor=S,side=RIGHT,padx=5,pady=5)
exitbutton.pack(anchor=S,side=RIGHT,padx=5,pady=5)
win.mainloop()
建立含图像的按钮:
from tkinter import *
def showmessage():
label.config(text="I like tkinter",bg="yellow",fg="black")
win=Tk()
label=Label(win)
imggif=PhotoImage(file=r"C:\Users\Administrator\Downloads\img.gif")
btn=Button(win,image=imggif,command=showmessage)
label.pack()
btn.pack()
win.mainloop()
实现简单的计算器布局:
from tkinter import *
win=Tk()
label=Label(win,text="",bg="yellow",width=20)
label.grid(row=0,column=0,columnspan=4)
button1=Button(win,text="7",width=3)
button2=Button(win,text="8",width=3)
button3=Button(win,text="9",width=3)
button4=Button(win,text="*",width=3)
button5=Button(win,text="4",width=3)
button6=Button(win,text="5",width=3)
button7=Button(win,text="6",width=3)
button8=Button(win,text="-",width=3)
button9=Button(win,text="1",width=3)
button10=Button(win,text="2",width=3)
button11=Button(win,text="3",width=3)
button12=Button(win,text="+",width=3)
button13=Button(win,text="0",width=8)
button14=Button(win,text=".",width=3)
button15=Button(win,text="=",width=3)
button1.grid(row=1,column=0,padx=5)
button2.grid(row=1,column=1,padx=5)
button3.grid(row=1,column=2,padx=5)
button4.grid(row=1,column=3,padx=5)
button5.grid(row=2,column=0,padx=5)
button6.grid(row=2,column=1,padx=5)
button7.grid(row=2,column=2,padx=5)
button8.grid(row=2,column=3,padx=5)
button9.grid(row=3,column=0,padx=5)
button10.grid(row=3,column=1,padx=5)
button11.grid(row=3,column=2,padx=5)
button12.grid(row=3,column=3,padx=5)
button13.grid(row=4,column=0,padx=5,columnspan=2)
button14.grid(row=4,column=2,padx=5)
button15.grid(row=4,column=3,padx=5)
win.mainloop()