按鈕的使用方法Button(父對(duì)象,options......)
options的取值borderwidth或bd:邊界寬度默認(rèn)是兩個(gè)像素;bg:背景顏色;command:點(diǎn)擊按鈕時(shí),執(zhí)行此方法;cursor:鼠標(biāo)移動(dòng)到按鈕上的形狀;fg:前景色。font:字形;height:高;highlightbackground:按鈕獲得焦點(diǎn)時(shí)的背景顏色;highlightcolor:按鈕取得焦點(diǎn)時(shí)的顏色;image:按鈕上的圖像;justify:當(dāng)有多行文字時(shí),最后一行文字的對(duì)齊方式;padx/pady:默認(rèn)是1,可以設(shè)置按鈕與文字的間隔以及按鈕的上下間距;relief:默認(rèn)是relief=FLAT,可以控制文字外框;state:默認(rèn)是state=NORMAL,如果設(shè)置為DISABLED則以灰階顯示功能按鈕,表示暫時(shí)無(wú)法使用;text:按鈕的名稱(chēng);underline:設(shè)置第幾個(gè)文字有下劃線,從0開(kāi)始算起,默認(rèn)-1是無(wú)下劃線;width:寬,單位是字符寬;wraplength:限制每行的文字?jǐn)?shù),默認(rèn)是0,表示只有\(zhòng)n才會(huì)換行。
創(chuàng)新互聯(lián)建站專(zhuān)注于壽寧企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,商城系統(tǒng)網(wǎng)站開(kāi)發(fā)。壽寧網(wǎng)站建設(shè)公司,為壽寧等地區(qū)提供建站服務(wù)。全流程定制設(shè)計(jì),專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)
32-設(shè)置一個(gè)按鈕,當(dāng)點(diǎn)擊該按鈕時(shí),可以顯示字符串“I LIKE TKINTER”,背景顏色是藍(lán)色,字符串的顏色是白色。
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="點(diǎn)我打印消息",command=show)
label.pack()
button.pack()
win.mainloop()
33-設(shè)置一個(gè)點(diǎn)擊取消可以關(guān)閉的窗口。
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-設(shè)計(jì)一個(gè)計(jì)時(shí)器程序,添加一個(gè)結(jié)束按鈕,當(dāng)點(diǎn)擊結(jié)束按鈕時(shí),程序執(zhí)行結(jié)束。
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="結(jié)束",width=15,command=win.destroy).pack()
win.mainloop()
35-設(shè)置三個(gè)按鈕點(diǎn)擊blue按鈕可以將窗口背景顏色設(shè)置為藍(lán)色,點(diǎn)擊red按鈕可以將窗口顏色設(shè)置為紅色,點(diǎn)擊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()
實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器布局:
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()