如何在 ttk.Button 中更改字型大小?
Tkinter 的 **Ttk** 是 Tkinter 中的一個原生庫,用於設定 Tkinter 應用程式中視窗部件的樣式。它為應用程式中定義的所有視窗部件提供了一個原生的 GUI 介面。為了使用 **ttk** 設定視窗部件的樣式,我們必須使用命令“**from tkinter import ttk**”將其匯入到筆記本中。
對於特定的應用程式,我們可以透過定義 ttk 樣式物件的例項來更改 **字型** 屬性,例如背景顏色、前景顏色、字型大小、字體系列和字型樣式。初始化 **ttk** 物件後,我們可以 **配置(options)** 應用程式中定義的每個視窗部件。
示例
在這個例子中,我們將建立一個按鈕,在定義樣式物件後可以對其進行自定義。
#Import the required Libraries from tkinter import * from tkinter import ttk #Create an instance of tkinter frame win = Tk() #Set the geometry of tkinter frame win.geometry("750x270") #Create an instance of Style Object style = ttk.Style() #Create ttk buttons small_button = ttk.Button(win, text="small button", style="small.TButton") small_button.pack(pady=20) big_button = ttk.Button(win, text="big button", style="big.TButton") big_button.pack() #Configure the properties of the Buttons style.configure('big.TButton', font=(None, 20), foreground="blue4") style.configure('small.TButton', font=(None, 7)) win.mainloop()
輸出
執行上面的程式碼將顯示一個視窗,其中包含兩個大小和屬性不同的按鈕。
在給定的輸出中,有兩個 **ttk** 按鈕,它們具有不同的屬性,例如字型大小和顏色。我們可以透過更新配置中的值來修改字型大小。
廣告