日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tkinter-button详解

發(fā)布時間:2023/12/8 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tkinter-button详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文介紹tkinter的button控件。通常按鈕的作用就是用來點擊,然后完成相應的動作。那么怎樣表示按鈕的作用以及對應的動作呢?本文將詳細介紹。

文章目錄

  • 按鈕(button)
    • 按鈕的屬性
      • text
      • font
      • width
      • height
      • anchor
      • image
      • compound
      • bitmap
      • backgroud\bg
      • foreground\fg
      • cursor
      • borderwidth\bd
      • padx
      • pady
      • relief
      • overrelief
      • justify
      • state
      • activebackground
      • activeforeground
      • disabledforeground
      • command
      • textvariable
      • repeatdelay
      • repeatinterval

按鈕(button)

在tkinter中,button的定義如下:

Button(master=None, cnf={}, **kw)

按鈕的屬性

text

指定按鈕上的文字

from tkinter import Tk,Buttonmain_win = Tk() main_win.title('漁道的按鈕') width = 450 height = 450 main_win.geometry(f'{width}x{height}')text = "點我" Button(main_win, text=text).pack()main_win.mainloop()

font

指定按鈕上文字的字體

text = "點我" font=("Courier", 20, "bold") Button(main_win, text=text, font=font).pack()

width

指定按鈕的寬度。如果按鈕上顯示的是文字,則以字符個數為寬度單位;如果按鈕上顯示的是圖片,則以像素為寬度單位

height

指定按鈕的高度。如果按鈕上顯示的是文字,則以字符個數為高度單位;如果按鈕上顯示的是圖片,則以像素為高度單位

text = "點我" Button(main_win, text=text, width=20, height=10).pack()

anchor

錨點,用來指定文本或圖像在label顯示區(qū)的顯示位置。默認值是"center",可設置的值為’n’, ‘ne’, ‘e’, ‘se’, ‘s’, ‘sw’, ‘w’, ‘nw’; ‘e’、‘w’、‘s’、'n’分別表示東西南北。

# 這里我們將按鈕的寬和高設置為40和10,anchor為'se' # 最終的顯示效果是 '點我'在按鈕的東南方,也就是右下角; Button(main_win, anchor='se', width=40, height=10, bg='white', text='點我').pack()

image

指定在按鈕上顯示的圖片

png = PhotoImage(file='qq.png') Button(main_win, image=png).pack()

注意,生成的是一個帶圖片的按鈕。

如果,text和image屬性都指定了,將會顯示什么呢?按鈕將會僅顯示圖片

png = PhotoImage(file='qq.png') text = 'hello' Button(main_win, image=png, text=text, fg='red').pack()

compound

使按鈕中 文字和圖片都可以顯示. text和image的位置是相對于image的.例如,compound為bottom,就是text在上,image在下.

main_win.geometry(f'{800}x{800}') png = PhotoImage(file='button.png') text = '漁道的按鈕' fg='blue' Button(main_win, compound='bottom', image=png, text=text, fg=fg).pack() Button(main_win, compound='top', image=png, text=text, fg=fg).pack() Button(main_win, compound='center', image=png, text=text, fg=fg).pack() Button(main_win, compound='left', image=png, text=text, fg=fg).pack() Button(main_win, compound='right', image=png, text=text, fg=fg).pack()

bitmap

bitmap屬性也是用來顯示圖片。但是有點兒特殊,它是用來顯示位圖。它有兩種顯示來源。一種是tkinter內置的位圖;一種是用戶指定圖片。

# 顯示tkinter內置的位圖 bitmap_list = ['error', 'hourglass', 'questhead', 'info','question', 'warning', 'gray12', 'gray50', 'gray75'] for bitmap in bitmap_list:Button(main_win, bitmap=bitmap).pack()

bitmap屬性的值是字符串,字符串值只有兩種,一種就是前面使用的內置位圖的字符串,另一種是使用 @+xbm后綴的文件名。

重點:僅支持xbm格式且xbm的路徑要使用@開頭

from PIL import Image xbm = 'qq.xbm' # 將qq.png轉換位單通道的qq.xbm圖片 Image.open('qq.png').convert("1").save(xbm) Button(main_win, bitmap='@'+xbm).pack()

backgroud\bg

指定按鈕背景顏色

foreground\fg

指定按鈕前景顏色,即文字的顏色

text = "點我" Button(main_win, fg='white', bg='black', text=text).pack()

cursor

指定鼠標在按鈕上時的顯示樣式,樣式的值是’arrow’, ‘watch’, ‘cross’

Button(main_win, cursor="cross", width=40, height=10, text='點我', anchor='ne').pack()

由于不能將鼠標截圖,這里就不展示了,可自行運行程序觀察。

borderwidth\bd

指定按鈕邊框寬度

padx

指定水平方向上按鈕內容和邊框的間距

pady

指定垂直方向上按鈕內容和邊框的間距

main_win.geometry(f'{800}x{800}') fg = 'white' bg = 'black' text = '點我' w = 20 h = 10 Button(main_win, text=text, fg=fg, background=bg).pack() Button(main_win, text=text, fg=fg, background=bg,width=w, height=h).pack() # borderwidth Button(main_win, text=text, fg=fg, background=bg,width=w, height=h, borderwidth=10).pack() # padx pady Button(main_win, text=text, fg=fg, background=bg,width=w, height=h, borderwidth=10,padx = 10, pady = 10).pack()main_win.mainloop()

上面例子的顯示結果可以看出,如果設置bd、padx、pady,按鈕會相應變大.

relief

指定邊框樣式,默認樣式為’flat’,樣式的種類有:‘flat’, ‘groove’, ‘raised’, ‘ridge’, ‘solid’, ‘sunken’

如果bd的寬度變大,relief顯示的邊框也會跟著變大。

main_win.geometry(f'{600}x{800}') relief_list = ['flat', 'groove', 'raised', 'ridge', 'solid', 'sunken']for relief in relief_list:Button(main_win, text="點我", fg='red', background="green",width = 10, height=5, borderwidth=10, padx=10, pady=10, relief=relief).pack()

overrelief

當鼠標飄過按鈕時,按鈕的邊框的顯示樣式

text='點我' Button(main_win, text=text, bg='red', bd=5, width=10, height=10, overrelief='groove').pack()

justify

定義按鈕中文字的對齊方式,"left"左對齊,"center"居中,"right"右對齊

poem = '''將進酒李白 君不見黃河之水天上來,奔流到海不復回。 君不見高堂明鏡悲白發(fā),朝如青絲暮成雪。 人生得意須盡歡,莫使金樽空對月。 天生我材必有用,千金散盡還復來。 烹羊宰牛且為樂,會須一飲三百杯。 岑夫子,丹丘生,將進酒,杯莫停。 與君歌一曲,請君為我傾耳聽。 鐘鼓饌玉不足貴,但愿長醉不愿醒。 古來圣賢皆寂寞,惟有飲者留其名。 陳王昔時宴平樂,斗酒十千恣歡謔。 主人何為言少錢,徑須沽取對君酌。 五花馬、千金裘,呼兒將出換美酒,與爾同銷萬古愁。 ''' main_win.geometry(f'{800}x{800}') Button(main_win, text=poem, fg='white', justify="center", background="blue",borderwidth=10, padx=10, pady=10, relief='ridge').pack()Button(main_win, text=poem, fg='white', justify="left", background="blue",borderwidth=10, padx=10, pady=10, relief='ridge').pack()

state

指定button的狀態(tài),normal(默認)/active/disable

activebackground

指定button為active狀態(tài)時的背景顏色

activeforeground

指定button為active狀態(tài)時的前景顏色

Button(main_win, text='點我', bg='white', state='active', activeforeground='red', activebackground='green').pack()Button(main_win, text='點我', bg='white', state='normal', activeforeground='red', activebackground='green').pack()

disabledforeground

當button的狀態(tài)為DISABLE時,文字的顯示顏色

Button(main_win, text='點我', bg='white', state='disable', disabledforeground='red', activebackground='green').pack()Button(main_win, text='點我', bg='white', state='normal', activeforeground='red', activebackground='green').pack()

command

指定按鈕被點擊時調用的函數。按鈕非常重要的屬性,提升人機交互體驗。

def click_cb():messagebox.showinfo("title", "我是漁道") Button(main_win, command=click_cb, text='點我', bg='white').pack()

鼠標點擊 "點我"按鈕后,會彈出一個對話框,如下圖所示:

textvariable

顯示 StringVar變量的內容,作用是可以動態(tài)的更新label顯示的內容。

def click_cb(text_var):text_var.set("好壞哦")text_var = StringVar() text_var.set("點我")Button(main_win, command=lambda : click_cb(text_var), bg='white', textvariable=text_var).pack()

當點擊"點我"按鈕后,按鈕上的文字變成了"好壞哦"。可運行程序感受一下簡單的動態(tài)交互效果.

repeatdelay

重復延遲

表示點擊按鈕后,延遲repeatdelay(ms),做出相應動作.

def click_cb():print("xxx")text_var = StringVar() text_var.set("點我")# 點擊按鈕,即刻執(zhí)行click_cb() # Button(main_win, command=click_cb, bg='white', textvariable=text_var).pack() Button(main_win, repeatdelay=1000, command=click_cb, bg='white', textvariable=text_var).pack() # 點擊按鈕,延遲1秒 執(zhí)行click_cb()

repeatinterval

重復間隔

repeatinterval需要和repeatdelay一起使用才能湊效.作用就是長按按鈕時,間隔repeatinterval(ms),執(zhí)行相應動作.

def click_cb():print("xxx")text_var = StringVar() text_var.set("點我")# 長按按鈕,間隔300ms,執(zhí)行一次click_cb() Button(main_win, repeatdelay=1000, repeatinterval=300, command=click_cb, bg='white', textvariable=text_var).pack()

button的屬性就先介紹到這里,如果以后有新的發(fā)現,繼續(xù)追加!

總結

以上是生活随笔為你收集整理的tkinter-button详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。