added methods to CTkTextbox

This commit is contained in:
Tom Schimansky 2022-07-12 21:52:52 +02:00
parent 92de2c4183
commit acaeceb96d
5 changed files with 44 additions and 24 deletions

View File

@ -1,6 +1,6 @@
import tkinter import tkinter
import sys import sys
from typing import Union, Tuple, Callable, Literal from typing import Union, Tuple, Callable
from .ctk_canvas import CTkCanvas from .ctk_canvas import CTkCanvas
from ..theme_manager import ThemeManager from ..theme_manager import ThemeManager
@ -250,7 +250,7 @@ class CTkButton(CTkBaseClass):
if "image" in kwargs: if "image" in kwargs:
self.image = kwargs.pop("image") self.image = kwargs.pop("image")
self.set_image(self.image) require_redraw = True
if "compound" in kwargs: if "compound" in kwargs:
self.compound = kwargs.pop("compound") self.compound = kwargs.pop("compound")

View File

@ -1,3 +1,4 @@
import sys
import tkinter import tkinter
from .ctk_canvas import CTkCanvas from .ctk_canvas import CTkCanvas
@ -106,6 +107,10 @@ class CTkLabel(CTkBaseClass):
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode)) self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
def config(self, **kwargs):
sys.stderr.write("Warning: Use .configure() instead of .config()")
self.configure(**kwargs)
def configure(self, require_redraw=False, **kwargs): def configure(self, require_redraw=False, **kwargs):
if "anchor" in kwargs: if "anchor" in kwargs:
self.anchor = kwargs.pop("anchor") self.anchor = kwargs.pop("anchor")

View File

@ -28,12 +28,13 @@ class CTkTextbox(CTkBaseClass):
# color # color
self.fg_color = ThemeManager.theme["color"]["entry"] if fg_color == "default_theme" else fg_color self.fg_color = ThemeManager.theme["color"]["entry"] if fg_color == "default_theme" else fg_color
self.border_color = ThemeManager.theme["color"]["frame_border"] if border_color == "default_theme" else border_color self.border_color = ThemeManager.theme["color"]["frame_border"] if border_color == "default_theme" else border_color
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
# shape # shape
self.corner_radius = ThemeManager.theme["shape"]["frame_corner_radius"] if corner_radius == "default_theme" else corner_radius self.corner_radius = ThemeManager.theme["shape"]["frame_corner_radius"] if corner_radius == "default_theme" else corner_radius
self.border_width = ThemeManager.theme["shape"]["frame_border_width"] if border_width == "default_theme" else border_width self.border_width = ThemeManager.theme["shape"]["frame_border_width"] if border_width == "default_theme" else border_width
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color # text
self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
# configure 1x1 grid # configure 1x1 grid
@ -56,28 +57,18 @@ class CTkTextbox(CTkBaseClass):
height=0, height=0,
font=self.text_font, font=self.text_font,
highlightthickness=0, highlightthickness=0,
insertbackground=ThemeManager.single_color(("black", "white"), self._appearance_mode),
bg=ThemeManager.single_color(self.fg_color, self._appearance_mode), bg=ThemeManager.single_color(self.fg_color, self._appearance_mode),
**kwargs) **kwargs)
self.textbox.grid(row=0, column=0, padx=self.corner_radius, pady=self.corner_radius, rowspan=1, columnspan=1, sticky="nsew") self.textbox.grid(row=0, column=0, padx=self.corner_radius, pady=self.corner_radius, rowspan=1, columnspan=1, sticky="nsew")
self.bind('<Configure>', self.update_dimensions_event) self.bind('<Configure>', self.update_dimensions_event)
self.draw() self.draw()
def winfo_children(self):
""" winfo_children of CTkFrame without self.canvas widget,
because it's not a child but part of the CTkFrame itself """
child_widgets = super().winfo_children()
try:
child_widgets.remove(self.canvas)
return child_widgets
except ValueError:
return child_widgets
def set_scaling(self, *args, **kwargs): def set_scaling(self, *args, **kwargs):
super().set_scaling(*args, **kwargs) super().set_scaling(*args, **kwargs)
self.textbox.configure(font=self.apply_font_scaling(self.text_font))
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width), height=self.apply_widget_scaling(self._desired_height)) self.canvas.configure(width=self.apply_widget_scaling(self._desired_width), height=self.apply_widget_scaling(self._desired_height))
self.draw() self.draw()
@ -110,17 +101,36 @@ class CTkTextbox(CTkBaseClass):
outline=ThemeManager.single_color(self.border_color, self._appearance_mode)) outline=ThemeManager.single_color(self.border_color, self._appearance_mode))
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode)) self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
self.textbox.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode),
bg=ThemeManager.single_color(self.fg_color, self._appearance_mode),
insertbackground=ThemeManager.single_color(("black", "white"), self._appearance_mode))
self.canvas.tag_lower("inner_parts") self.canvas.tag_lower("inner_parts")
self.canvas.tag_lower("border_parts") self.canvas.tag_lower("border_parts")
def yview(self, args, kwargs): def yview(self, *args):
self.textbox.yview(args, kwargs) return self.textbox.yview(*args)
def xview(self, args, kwargs): def xview(self, *args):
self.textbox.xview(args, kwargs) return self.textbox.xview(*args)
def insert(self, args, kwargs): def insert(self, *args, **kwargs):
self.textbox.insert(args, kwargs) return self.textbox.insert(*args, **kwargs)
def focus(self):
return self.textbox.focus()
def tag_add(self, *args, **kwargs):
return self.textbox.tag_add(*args, **kwargs)
def tag_config(self, *args, **kwargs):
return self.textbox.tag_config(*args, **kwargs)
def tag_configure(self, *args, **kwargs):
return self.textbox.tag_configure(*args, **kwargs)
def tag_remove(self, *args, **kwargs):
return self.textbox.tag_remove(*args, **kwargs)
def configure(self, require_redraw=False, **kwargs): def configure(self, require_redraw=False, **kwargs):
if "fg_color" in kwargs: if "fg_color" in kwargs:

View File

@ -28,6 +28,7 @@ progressbar_1.pack(pady=12, padx=10)
button_1 = customtkinter.CTkButton(master=frame_1, command=button_callback) button_1 = customtkinter.CTkButton(master=frame_1, command=button_callback)
button_1.pack(pady=12, padx=10) button_1.pack(pady=12, padx=10)
button_1.configure(state='disabled')
slider_1 = customtkinter.CTkSlider(master=frame_1, command=slider_callback, from_=0, to=1) slider_1 = customtkinter.CTkSlider(master=frame_1, command=slider_callback, from_=0, to=1)
slider_1.pack(pady=12, padx=10) slider_1.pack(pady=12, padx=10)

View File

@ -39,7 +39,7 @@ class App(customtkinter.CTk):
self.appearance_mode_optionemenu.grid(row=6, column=0, padx=20, pady=(10, 10)) self.appearance_mode_optionemenu.grid(row=6, column=0, padx=20, pady=(10, 10))
self.scaling_label = customtkinter.CTkLabel(self.sidebar_frame, text="Widget Scaling:", anchor="w") self.scaling_label = customtkinter.CTkLabel(self.sidebar_frame, text="Widget Scaling:", anchor="w")
self.scaling_label.grid(row=7, column=0, padx=20, pady=(10, 0)) self.scaling_label.grid(row=7, column=0, padx=20, pady=(10, 0))
self.scaling_optionemenu = customtkinter.CTkOptionMenu(self.sidebar_frame, values=["75%", "100%", "150%"], self.scaling_optionemenu = customtkinter.CTkOptionMenu(self.sidebar_frame, values=["90%", "100%", "110%"],
command=self.change_scaling) command=self.change_scaling)
self.scaling_optionemenu.grid(row=8, column=0, padx=20, pady=(10, 20)) self.scaling_optionemenu.grid(row=8, column=0, padx=20, pady=(10, 20))
@ -50,8 +50,8 @@ class App(customtkinter.CTk):
self.main_button_1 = customtkinter.CTkButton(self, fg_color=None, border_width=2) self.main_button_1 = customtkinter.CTkButton(self, fg_color=None, border_width=2)
self.main_button_1.grid(row=3, column=3, padx=(10, 20), pady=(10, 20), sticky="nsew") self.main_button_1.grid(row=3, column=3, padx=(10, 20), pady=(10, 20), sticky="nsew")
self.text_frame = customtkinter.CTkFrame(self) self.textbox = customtkinter.CTkTextbox(self)
self.text_frame.grid(row=0, column=1, padx=(20, 10), pady=(20, 10), sticky="nsew") self.textbox.grid(row=0, column=1, padx=(20, 10), pady=(20, 10), sticky="nsew")
# create radiobutton frame # create radiobutton frame
self.radiobutton_frame = customtkinter.CTkFrame(self) self.radiobutton_frame = customtkinter.CTkFrame(self)
@ -119,6 +119,10 @@ class App(customtkinter.CTk):
self.scaling_optionemenu.set("100%") self.scaling_optionemenu.set("100%")
self.optionmenu_1.set("CTkOptionmenu") self.optionmenu_1.set("CTkOptionmenu")
self.combobox_1.set("CTkComboBox") self.combobox_1.set("CTkComboBox")
self.textbox.insert("1.0",
"CTkTextbox\n\nLorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.", )
#self.textbox.tag_add("headline", "1.0", "1.end")
#self.textbox.tag_config("headline", foreground="red")
def open_input_dialog(self): def open_input_dialog(self):
dialog = customtkinter.CTkInputDialog(master=None, text="Type in a number:", title="CTkInputDialog") dialog = customtkinter.CTkInputDialog(master=None, text="Type in a number:", title="CTkInputDialog")