如何在 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()输出
执行以上代码将显示一个窗口,窗口的背景色经过自定义。
现在,找到一个新的颜色代码来更改窗口的背景色。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP