如何在 tkinter 中使用 RGB 顏色程式碼?
Tkinter 有許多內建功能和屬性,可幫助應用程式開發人員構建強大且具有特色的應用程式。我們可以使用 Tkinter 中的配置方法來設定小部件的屬性,例如背景色、前景色和其他屬性。
要設定小部件的背景色或前景色,我們可以同時使用預設顏色和 RGB 顏色程式碼。RGB 由包含 R、G、B 值不同數字的 6 位字母數字字元定義。為了在 Tkinter 中使用 RGB 顏色程式碼,我們必須使用格式#aab123對其進行定義。
示例
# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame win = Tk() # Set the size of the tkinter window win.geometry("700x350") # Configure the color of the window win.configure(bg='#65A8E1') # Define the style for combobox widget style = ttk.Style() style.theme_use('xpnative') # Define an event to close the window def close_win(e): win.destroy() # Add a label widget label = ttk.Label(win, text="Eat, Sleep, Code and Repeat", font=('Times New Roman italic', 22), background="black", foreground="white") label.place(relx=.5, rely=.5, anchor=CENTER) win.mainloop()
輸出
執行以上程式碼將顯示一個視窗,視窗的背景色經過自定義。
現在,找到一個新的顏色程式碼來更改視窗的背景色。
廣告