Python Tkinter Button按鈕


Python Tkinter Button按鈕

  1. BUTTON 小工具時使用的按鈕添加到各種類型的Python應用。Python允許用戶配置按鈕的按我們的要求。各種選項可以被設置或重置的要求。

  2. 我們還可以將方法或功能的按鈕,當按鈕被按壓

  3. 語法

button = Button(parent,options)
  1. 可能選項的列表
選項 描述
activebackground 它表示背景的按鈕在按鈕上懸停鼠標的動作,改變按鈕背景色
activeforeground 它表示字體顏色的按鈕,當鼠標懸停在按鈕上改變前景色
bd 它表示按鈕的邊界寬度
command 它被設置為回調函數時,可以調用該函數
bg 設置按鈕背景的顏色
b 設置按鈕的前景顏色
font 設置按鈕文本的字體屬性
height 設置按鈕的高度
width 按鈕的寬度,它以文本按鈕的字母數或圖像按鈕的像素數的形式存在
image 設置圖像上顯示的按鈕
padx 按鈕在水平方向上的附加填充
pady 按鈕在垂直方向上的附加填充
justify 設置多個文本行為左對齊、右對齊或者居中對齊
highlightcolor 設置當按鈕具有焦點突出顯示的顏色
relief 它表示邊框的類型,可以設置SUNKEN, RAISED, GROOVE, and RIDGE
state 將此選項設置為禁用,以使按鈕無響應。活動表示按鈕的活動狀態
underline 設定此選項以使按鈕文本下划線
wraplength 如果該值被設置為正數,文本行將被包裝成適合這個長度
  1. 示例一
from tkinter import *   
root = Tk()  
root.geometry("200x100")  
b = Button(root, text = "Simple")  
b.pack()  
root.mainaloop()
  • 輸出

  1. 示例二
from tkinter import *   
root = Tk()  
root.geometry("200x100")  
def fun():  
    messagebox.showinfo("Hello", "Red Button clicked")
  
b1 = Button(root,text = "Red",command = fun,activeforeground = "red",activebackground = "pink",pady=10)  
b2 = Button(root, text = "Blue",activeforeground = "blue",activebackground = "pink",pady=10)  
b3 = Button(root, text = "Green",activeforeground = "green",activebackground = "pink",pady = 10)  
b4 = Button(root, text = "Yellow",activeforeground = "yellow",activebackground = "pink",pady = 10)  
b1.pack(side = LEFT)  
b2.pack(side = RIGHT)  
b3.pack(side = TOP)  
b4.pack(side = BOTTOM)  
root.mainloop()
  • 輸出


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM