Tkinter是 Python 中用于开发图形用户界面 (GUI) 的内置模块。它允许我们开发桌面应用程序。Tkinter 非常简单且易于使用。它为我们提供了不同的小部件,例如button, canvas, label, menu, message, etc.
用 Python 构建 GUI。
您还可以探索我们所有的Tkinter 教程!本教程将向您介绍 Tkinter 中不同颜色选项的使用。
Tkinter 中的颜色是什么?
颜色主要用于使 GUI 更具吸引力。Tkinter 将颜色视为字符串。颜色可以通过两种方式提及:
- 十六进制值
例如 #FF0000(红色)、#008000(绿色)、#FFFF00(黄色)等。 - 颜色名称
例如 金色、银色、蓝色等
以下是供快速参考的颜色代码列表:
现在,让我们探索 Tkinter 中可用的不同颜色选项。
Tkinter 中的颜色选项
首先,让我们看看我们将用来探索所有颜色选项的 GUI 的总体结构。
import tkinter as tk from tkinter import * #main window root = tk.Tk() #title of the window root.title( "Tkinter Colors" ) #disabling resizing of the window root.resizable( 0 , 0 ) #---frame for top name--- top = Frame(root, width = 500 , height = 70 , bd = 8 , relief = "raise" ) top.pack(side = TOP) #--name in the frame-- name = Label(top, text = "Colour Option Name" , width = 30 , height = 2 , font = ( "Lucida Console" , 14 , "italic" )) name.grid(padx = 18 ) #--button-- button = Button(top, text = "Click Here" , width = 20 , height = 3 ) button.grid(padx = 2 ) root.mainloop() |
输出:
上面是一个基本的 GUI,标题为“Tkinter Colors”。它由顶部的一个框架(将显示颜色选择名称)和其下方的一个按钮(用于演示功能)组成。
如果将光标指向某个元素或小部件并按下鼠标按钮,它将执行某些操作,则该元素或小部件是活动元素。
1. 活动背景
我们可以在活动元素上使用此选项来设置小部件处于活动状态时的背景颜色。
import tkinter as tk from tkinter import * #main window root = tk.Tk() #title of the window root.title( "Tkinter Colors" ) #disabling resizing of the window root.resizable( 0 , 0 ) #---frame for top name--- top = Frame(root, width = 500 , height = 70 , bd = 8 , relief = "raise" ) top.pack(side = TOP) #--name in the frame-- name = Label(top, text = "Active Background" , width = 30 , height = 2 , font = ( "Lucida Console" , 14 , "italic" )) name.grid(padx = 18 ) #--button-- button = Button(top, text = "Click Here" , width = 20 , height = 3 , activebackground = 'red' ) button.grid(padx = 2 ) root.mainloop() |
输出:
上面的视频显示,当单击按钮(即处于活动状态)时,按钮背景会变成红色。这就是activebackground
工作原理。
2. 活跃前景
此选项指定小部件处于活动状态时的前景色,即激活小部件将使颜色更改为代码中指定的颜色。
import tkinter as tk from tkinter import * #main window root = tk.Tk() #title of the window root.title( "Tkinter Colors" ) #disabling resizing of the window root.resizable( 0 , 0 ) #---frame for top name--- top = Frame(root, width = 500 , height = 70 , bd = 8 , relief = "raise" ) top.pack(side = TOP) #--name in the frame-- name = Label(top, text = "Active Foreground" , width = 30 , height = 2 , font = ( "Lucida Console" , 14 , "italic" )) name.grid(padx = 18 ) #--button-- button = Button(top, text = "Click Here" , width = 20 , height = 3 , font = ( "Lucida Console" , 12 , "bold" ), activeforeground = 'red' ) button.grid(padx = 2 ) root.mainloop() |
输出: 3。
如上面的视频所示,当我们单击按钮时,文本“单击此处”的颜色将变为红色,如代码中所述。
3、背景
该颜色表示小部件的背景颜色。它可用于活动和非活动元素。背景也可以指定为bg
。在这里,我们没有使用按钮,而是使用标签作为小部件来演示该选项。
import tkinter as tk from tkinter import * #main window root = tk.Tk() #title of the window root.title( "Tkinter Colors" ) #disabling resizing of the window root.resizable( 0 , 0 ) #---frame for top name--- top = Frame(root, width = 500 , height = 70 , bd = 8 , relief = "raise" ) top.pack(side = TOP) #--name in the frame-- name = Label(top, text = "Background" , width = 30 , height = 2 , font = ( "Andalus" , 14 )) name.grid(padx = 18 ) #--label-- label = Label(top, text = "Label background" , width = 20 , height = 3 , borderwidth = 5 , relief = "solid" , font = ( "Lucida Console" , 12 , "bold" ), background = 'yellow' ) label.grid(padx = 2 ) root.mainloop() |
输出:
这里标签的背景是黄色的。
4.前景
该颜色表示小部件的前景色。它也可用于活动和非活动元素。前景也可以指定为fg
。在这里,我们也使用标签作为小部件来演示该选项,而不是按钮。
import tkinter as tk from tkinter import * #main window root = tk.Tk() #title of the window root.title( "Tkinter Colors" ) #disabling resizing of the window root.resizable( 0 , 0 ) #---frame for top name--- top = Frame(root, width = 500 , height = 70 , bd = 8 , relief = "raise" ) top.pack(side = TOP) #--name in the frame-- name = Label(top, text = "Foreground" , width = 30 , height = 2 , font = ( "Andalus" , 14 )) name.grid(padx = 18 ) #--label-- label = Label(top, text = "Label foreground" , width = 20 , height = 3 , borderwidth = 5 , relief = "solid" , font = ( "Lucida Console" , 12 , "bold" ), foreground = 'blue' ) label.grid(padx = 2 ) root.mainloop() |
输出:
正如我们所看到的,这里标签的前景色是蓝色。
结论
这就是本教程的内容。我们已经了解了 Tkinter 中可用的不同颜色选项。