This is a simple example demonstrating how to create a label using the CustomTkinter library.
import customtkinter
# Main class
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.geometry("400x200")
self.title("Creating Label")
# Add Label
self.label = customtkinter.CTkLabel(self, text="CTkLabel")
self.label.place(relx=0.5, rely=0.5, anchor="center")
# Initializing the App
app = App()
app.mainloop()
This code creates a window using the CustomTkinter library and adds a label to the window. The label is created using the CTkLabel
class from the CustomTkinter library and is positioned in the center of the window using the place()
method.
The App
class is defined as a subclass of CTk
, the main class of the CustomTkinter library. In the __init__
method, the window is configured with a geometry of 400x200
pixels and a title of "Creating Label". Then, a label is created using the CTkLabel
class and added to the window using the place()
method. The label is centered in the window using the options relx=0.5
, rely=0.5
, and anchor="center"
.
Finally, the App
object is initialized, and the main loop of the window is executed using the mainloop()
method. When the loop runs, the window is displayed on the screen, and the user can interact with the label and other widgets added to the window.
How to Customize a Label in CustomTkinter
You can further customize the label by adding text formatting options, fonts, colors, and other features available in the CustomTkinter library.
import customtkinter
import tkinter
# Main class
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.geometry("400x200")
self.title("Creating Label")
# Add Label
text_variable = tkinter.StringVar(value="CTkLabel")
label = customtkinter.CTkLabel(self,
textvariable=text_variable,
width=120,
height=25,
fg_color=("white", "gray75"),
corner_radius=8)
label.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
# Initializing the App
app = App()
app.mainloop()
This code creates a window using the CustomTkinter library and adds a customized label to the window. The label is created using the CTkLabel
class from the CustomTkinter library and is positioned in the center of the window using the place()
method.
In the __init__
method of the App
class, the window is configured with a geometry of 400x200
pixels and a title of "Creating Label". Then, a text variable is created using the StringVar
class from Tkinter and is set with an initial value of "CTkLabel".
The label is created using the CTkLabel
class and configured with the following options:
-
textvariable: Specifies the previously created text variable as the text to be displayed on the label.
-
width: Specifies the width of the label in pixels.
-
height: Specifies the height of the label in pixels.
-
fg_color: Specifies a tuple of two colors that will be used as foreground and background colors for the label.
-
corner_radius: Specifies the radius of the label's rounded corners.
The label is then positioned in the center of the window using the options relx=0.5
, rely=0.5
, and anchor=tkinter.CENTER
in the place()
method.
Finally, the App
object is initialized, and the main loop of the window is executed using the mainloop()
method. When the loop runs, the window is displayed on the screen with the customized label, and the user can interact with the label and other widgets added to the window.
This is a more advanced example demonstrating how to create a customized label using the CustomTkinter library. You can further customize the label by adding additional text formatting options, fonts, colors, and other features available in the CustomTkinter library.
Label Arguments in CustomTkinter
Argument | Value |
---|---|
master |
root , tkinter.Frame or CTkFrame |
textvariable |
tkinter.StringVar object |
text |
String |
width |
Label width in px |
height |
Label height in px |
corner_radius |
Corner radius in px |
fg_color |
Foreground color, tuple: (light_color, dark_color) , single color, or "transparent" |
text_color |
Label text color, tuple: (light_color, dark_color) , or single color |
font |
Label text font, tuple: (font_name, size) |
anchor |
Controls text positioning if the widget has more space than needed; default is "center" |
compound |
Controls image positioning relative to text; default is "center", other options: "top", "bottom", "left", "right" |
justify |
Specifies how multiple lines of text are aligned: "left" for left-aligned, "center" for centered (default), or "right" for right-aligned |
padx |
Extra space added to the left and right of the text, default is 1 |
pady |
Extra space added above and below the text, default is 1 |
Label Methods in CustomTkinter
-
.configure(attribute=value, ...)
-
All attributes can be configured and updated.
ctk_label.configure(text=new_text)
-
-
.cget(attribute_name)
-
Pass the attribute name as a string and get the current value of the attribute.
text = ctk_label.cget("text")
-
-
.bind(sequence=None, command=None, add=None)
-
Bind events to the label.
-
Copyright Statement: Unless stated otherwise, all articles are original to this site, please credit the source when sharing.
Article link:http://pybeginners.com/customtkinter/python-customtkinter-how-to-create-a-label-in-customtkinter/
License Agreement:Attribution-NonCommercial 4.0 International License