如何更改 Tkinter LableFrame 边框颜色?
Tkinter 中的LableFrame是创建包含其他小部件的矩形区域的小部件。在本文中,我们将了解如何更改标签框的边框。但是为了实现颜色,我们需要通过 Tkinter 的主题,因此我们使用Python 3 Tkinter 中内置的ttk 模块。
分步实施:
第 1 步:导入所有必需的模块
Python3
# import tkinter
import tkinter as tk
# import ttk theme module for styling
import tkinter.ttk as ttk
Python3
import tkinter as tk
import tkinter.ttk as ttk
# initialize the tkinter window
root = tk.Tk()
# provide some width and height
root.geometry("300x300")
# created a label frame with title "Group"
labelframe = ttk.LabelFrame(root, text = "GFG")
# provide padding
labelframe.pack(padx=30, pady=30)
# created the text label inside the the labelframe
left = tk.Label(labelframe, text="Geeks for Geeks")
left.pack()
root.mainloop()
Python3
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
# initialize style function
style = ttk.Style()
# Use clam theme
style.theme_use('clam')
# Used TLabelframe for styling labelframe widgets,
# and use red color for border
style.configure("TLabelframe", bordercolor="red")
labelframe = ttk.LabelFrame(root, text = "GFG")
labelframe.grid(padx = 30, pady = 30)
left = tk.Label(labelframe, text = "Geeks for Geeks")
left.pack()
root.mainloop()
Python3
import tkinter as tk
import tkinter.ttk as ttk
# initialize the tkinter window
root = tk.Tk()
# initializing the style function
style = ttk.Style()
# creating the theme with the
# initializing the style function
style = ttk.Style()
# creating the theme with the
style.theme_create('style_class',
# getting the settings
settings={
# getting through the Labelframe
# widget
'TLabelframe': {
# configure the changes
'configure': {
'background': 'green'
}
},
# getting through the Labelframe's
# label widget
'TLabelframe.Label': {
'configure': {
'background': 'green'
}
}
}
)
style.theme_use('style_class')
# created a label frame with title "Group"
labelframe = ttk.LabelFrame(root, text="Group")
# provide padding
labelframe.pack(padx=30, pady=30)
# created the text label inside the the labelframe
left = tk.Label(labelframe, text="Geeks for Geeks")
left.pack()
root.mainloop()
第 2 步:构建标签框架并在其中放置一些小部件。
蟒蛇3
import tkinter as tk
import tkinter.ttk as ttk
# initialize the tkinter window
root = tk.Tk()
# provide some width and height
root.geometry("300x300")
# created a label frame with title "Group"
labelframe = ttk.LabelFrame(root, text = "GFG")
# provide padding
labelframe.pack(padx=30, pady=30)
# created the text label inside the the labelframe
left = tk.Label(labelframe, text="Geeks for Geeks")
left.pack()
root.mainloop()
输出:
示例 1:在 Tkinter 中使用 Clam 主题。
Clam 是 tkinter 窗口和小部件的主题。它属于 ttk 模块。创建clam ttk窗口的语法:
style = ttk.Style()
style.theme_use('clam')
注意:我们在 ttk 模块中使用了蛤蜊主题,它可以使用边框颜色等属性。其他主题可能没有像边框颜色这样的属性。
下面是实现:
蟒蛇3
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
# initialize style function
style = ttk.Style()
# Use clam theme
style.theme_use('clam')
# Used TLabelframe for styling labelframe widgets,
# and use red color for border
style.configure("TLabelframe", bordercolor="red")
labelframe = ttk.LabelFrame(root, text = "GFG")
labelframe.grid(padx = 30, pady = 30)
left = tk.Label(labelframe, text = "Geeks for Geeks")
left.pack()
root.mainloop()
输出:
示例 2:构建我们自己的样式功能主题。
句法:
# creating the theme
style.theme_create(‘class_name’, # Name of the class
settings={
‘TLabelframe’: { # Name of the widget
‘configure’: { # Configure argument
‘background’:’green’ # changing background argument
}
}
}
)
# For using the theme
style.theme_use(‘class_name’)
我们借助 ttk 样式创建了样式主题函数。我们使用theme_create来创建功能,并且可以使用theme_use来分配主题。
下面是实现:
蟒蛇3
import tkinter as tk
import tkinter.ttk as ttk
# initialize the tkinter window
root = tk.Tk()
# initializing the style function
style = ttk.Style()
# creating the theme with the
# initializing the style function
style = ttk.Style()
# creating the theme with the
style.theme_create('style_class',
# getting the settings
settings={
# getting through the Labelframe
# widget
'TLabelframe': {
# configure the changes
'configure': {
'background': 'green'
}
},
# getting through the Labelframe's
# label widget
'TLabelframe.Label': {
'configure': {
'background': 'green'
}
}
}
)
style.theme_use('style_class')
# created a label frame with title "Group"
labelframe = ttk.LabelFrame(root, text="Group")
# provide padding
labelframe.pack(padx=30, pady=30)
# created the text label inside the the labelframe
left = tk.Label(labelframe, text="Geeks for Geeks")
left.pack()
root.mainloop()
输出: