The CTkTextbox is a widget used to create text areas in CustomTkinter. It supports vertical and horizontal scrolling (when configured with wrap='none') and allows text manipulation using methods like insert, get, and delete.
The indices used to manipulate the text are based on tkinter.Text. More information about index formatting can be found in the official Tkinter Text widget documentation
Basic Example (Without Classes)
import customtkinter
# Initial configuration
app = customtkinter.CTk()
app.geometry("400x300")
# Creating a text box
textbox = customtkinter.CTkTextbox(app, width=300, height=200)
textbox.pack(padx=10, pady=10)
# Inserting, retrieving, and deleting text
textbox.insert("0.0", "Initial text inserted into the textbox.")
text = textbox.get("0.0", "end") # Getting the text
print("Text in the textbox:", text)
textbox.delete("0.0", "end") # Removing the text
textbox.insert("0.0", "Updated text.") # Re-inserting text
textbox.configure(state="disabled") # Setting textbox to read-only
app.mainloop()
Example Using Classes
Here's an example where the CTkTextbox fills the entire window:
import customtkinter
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.geometry("500x400")
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
# Creating the CTkTextbox
self.textbox = customtkinter.CTkTextbox(master=self, width=400, corner_radius=0)
self.textbox.grid(row=0, column=0, sticky="nsew")
# Inserting sample text
self.textbox.insert("0.0", "Sample text in the textbox.\n" * 20)
app = App()
app.mainloop()
CTkTextbox Parameters
| Argument | Description |
|---|---|
master |
Required. Parent widget (root, Frame, or TopLevel). |
width |
Width of the textbox in pixels. |
height |
Height of the textbox in pixels. |
corner_radius |
Radius of the rounded corners in pixels. |
border_width |
Border thickness in pixels. |
border_spacing |
Minimum spacing between text and border (default: 3). |
fg_color |
Background color (single value or tuple: (light_color, dark_color)). |
border_color |
Border color (single value or tuple). |
text_color |
Text color (single value or tuple). |
scrollbar_button_color |
Scrollbar main color. |
scrollbar_button_hover_color |
Scrollbar hover color. |
font |
Font as a tuple, e.g., ("Arial", 12). |
activate_scrollbars |
Enables/disables scrollbars (default: True). |
state |
Sets the state as "normal" (editable) or "disabled" (read-only). |
wrap |
Line wrapping mode: "char", "word", or "none". |
Useful Methods
| Method | Description |
|---|---|
.configure() |
Updates widget properties (e.g., state="disabled"). |
.cget() |
Gets the current value of a given property (e.g., textbox.cget("state")). |
.bind() |
Binds an event to a specific action. |
.unbind() |
Unbinds a previously bound action. |
.insert() |
Inserts text at a given index (e.g., textbox.insert("0.0", "Text")). |
.delete() |
Deletes text between two indices (e.g., textbox.delete("0.0", "end")). |
.get() |
Retrieves text between two indices (e.g., textbox.get("0.0", "end")). |
.focus_set() |
Sets the focus on the widget. |
With these examples and arguments, you can create highly customizable text areas to suit the needs of your project using CustomTkinter.
Copyright statement: Unless otherwise indicated, all articles are original to this site, please cite the source when sharing.
Article link:http://pybeginners.com/customtkinter/ctktextbox-in-customtkinter/
License agreement:Creative Commons Attribution-NonCommercial 4.0 International License