您好,登錄后才能下訂單哦!
按鈕的使用方法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()
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。