mirror of
https://github.com/TomSchimansky/CustomTkinter.git
synced 2023-08-10 21:13:13 +03:00
Compare commits
44 Commits
Author | SHA1 | Date | |
---|---|---|---|
9c479bc1de | |||
ecf6b8d9cf | |||
807064a888 | |||
550653c6c3 | |||
0aa9dfc70f | |||
a9f51f1aa1 | |||
2746e2a05f | |||
f5fdd77584 | |||
940ed128bd | |||
60b13bf215 | |||
a50e2ea9ca | |||
15558b4d0f | |||
aa46c56da9 | |||
cf6f513afc | |||
9d618386e1 | |||
9bd55cc159 | |||
8a87b6f926 | |||
a1afc3056b | |||
34da9505e9 | |||
91a8687736 | |||
e96165d212 | |||
aa8c96a2c4 | |||
fd8135129c | |||
5f88db11aa | |||
1fed35a193 | |||
4b3b406250 | |||
f49c83d2dc | |||
4389c3e86b | |||
25297c2598 | |||
4e155aedd6 | |||
3a5d34cef6 | |||
e42db49ca5 | |||
a7c0fc2a3c | |||
9be2a76b25 | |||
b1ac3b6d45 | |||
a6b563abb1 | |||
152a2a9652 | |||
0da9a17b55 | |||
8cafed1b06 | |||
0c7abdaec5 | |||
a1cd4abddd | |||
eeeb85ec0d | |||
df1420cd02 | |||
856aa2e1a8 |
18
CHANGELOG.md
18
CHANGELOG.md
@ -1,6 +1,20 @@
|
||||
# Changelog
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
*This document is a WIP.*
|
||||
## [4.1.0] - 2022-05-24
|
||||
### Added
|
||||
- Configure width and height for frame, button, label, progressbar, slider, entry
|
||||
|
||||
## [4.0.0] - 2022-05-22
|
||||
### Added
|
||||
- This changelog file
|
||||
- Adopted semantic versioning
|
||||
- Added HighDPI scaling to all widgets and geometry managers (place, pack, grid)
|
||||
- Restructured CTkSettings and renamed a few manager classes
|
||||
- Orientation attribute for slider and progressbar
|
||||
|
||||
### Removed
|
||||
- A few unnecessary tests
|
||||
|
11
Readme.md
11
Readme.md
@ -16,7 +16,8 @@ CustomTkinter is a python UI-library based on Tkinter, which provides new, moder
|
||||
fully customizable widgets. They are created and used like normal Tkinter widgets and
|
||||
can also be used in combination with normal Tkinter elements. The widgets
|
||||
and the window colors either adapt to the system appearance or the manually set mode
|
||||
('light', 'dark'). With CustomTkinter you'll get a consistent and modern look across all
|
||||
('light', 'dark'), and all CustomTkinter widgets and windows support HighDPI scaling
|
||||
(Windows, macOS). With CustomTkinter you'll get a consistent and modern look across all
|
||||
desktop platforms (Windows, macOS, Linux).
|
||||
|
||||
|
||||
@ -41,17 +42,17 @@ import customtkinter
|
||||
customtkinter.set_appearance_mode("System") # Modes: system (default), light, dark
|
||||
customtkinter.set_default_color_theme("blue") # Themes: blue (default), dark-blue, green
|
||||
|
||||
root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window
|
||||
root_tk.geometry("400x240")
|
||||
app = customtkinter.CTk() # create CTk window like you do with the Tk window
|
||||
app.geometry("400x240")
|
||||
|
||||
def button_function():
|
||||
print("button pressed")
|
||||
|
||||
# Use CTkButton instead of tkinter Button
|
||||
button = customtkinter.CTkButton(master=root_tk, text="CTkButton", command=button_function)
|
||||
button = customtkinter.CTkButton(master=app, text="CTkButton", command=button_function)
|
||||
button.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
|
||||
|
||||
root_tk.mainloop()
|
||||
app.mainloop()
|
||||
```
|
||||
which gives the following (macOS dark mode on):
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
__version__ = "4.0.0"
|
||||
__version__ = "4.3.0"
|
||||
|
||||
import os
|
||||
import sys
|
||||
from tkinter.constants import *
|
||||
from tkinter import StringVar, IntVar, DoubleVar, BooleanVar
|
||||
|
||||
# import manager classes
|
||||
from .settings import Settings
|
||||
@ -21,13 +23,16 @@ if sys.platform == "darwin":
|
||||
else:
|
||||
DrawEngine.preferred_drawing_method = "font_shapes"
|
||||
|
||||
if sys.platform.startswith("win") and sys.getwindowsversion().build < 9000: # No automatic scaling on Windows < 8.1
|
||||
ScalingTracker.deactivate_automatic_dpi_awareness = True
|
||||
|
||||
# load Roboto fonts (used on Windows/Linux)
|
||||
script_directory = os.path.dirname(os.path.abspath(__file__))
|
||||
FontManager.load_font(os.path.join(script_directory, "assets", "fonts", "Roboto", "Roboto-Regular.ttf"))
|
||||
FontManager.load_font(os.path.join(script_directory, "assets", "fonts", "Roboto", "Roboto-Medium.ttf"))
|
||||
|
||||
# load font necessary for rendering the widgets (used on Windows/Linux)
|
||||
if FontManager.load_font(os.path.join(script_directory, "assets", "fonts", "CustomTkinter_shapes_font-fine.otf")) is False:
|
||||
if FontManager.load_font(os.path.join(script_directory, "assets", "fonts", "CustomTkinter_shapes_font.otf")) is False:
|
||||
# change draw method if font loading failed
|
||||
if DrawEngine.preferred_drawing_method == "font_shapes":
|
||||
sys.stderr.write("customtkinter.__init__ warning: " +
|
||||
@ -46,6 +51,8 @@ from .widgets.ctk_label import CTkLabel
|
||||
from .widgets.ctk_radiobutton import CTkRadioButton
|
||||
from .widgets.ctk_canvas import CTkCanvas
|
||||
from .widgets.ctk_switch import CTkSwitch
|
||||
from .widgets.ctk_optionmenu import CTkOptionMenu
|
||||
from .widgets.ctk_combobox import CTkComboBox
|
||||
|
||||
# import windows
|
||||
from .windows.ctk_tk import CTk
|
||||
|
@ -19,7 +19,7 @@ except Exception:
|
||||
class AppearanceModeTracker:
|
||||
|
||||
callback_list = []
|
||||
root_tk_list = []
|
||||
app_list = []
|
||||
update_loop_running = False
|
||||
update_loop_interval = 500 # milliseconds
|
||||
|
||||
@ -40,12 +40,12 @@ class AppearanceModeTracker:
|
||||
cls.callback_list.append(callback)
|
||||
|
||||
if widget is not None:
|
||||
root_tk = cls.get_tk_root_of_widget(widget)
|
||||
if root_tk not in cls.root_tk_list:
|
||||
cls.root_tk_list.append(root_tk)
|
||||
app = cls.get_tk_root_of_widget(widget)
|
||||
if app not in cls.app_list:
|
||||
cls.app_list.append(app)
|
||||
|
||||
if not cls.update_loop_running:
|
||||
root_tk.after(500, cls.update)
|
||||
app.after(500, cls.update)
|
||||
cls.update_loop_running = True
|
||||
|
||||
@classmethod
|
||||
@ -97,9 +97,9 @@ class AppearanceModeTracker:
|
||||
cls.update_callbacks()
|
||||
|
||||
# find an existing tkinter.Tk object for the next call of .after()
|
||||
for root_tk in cls.root_tk_list:
|
||||
for app in cls.app_list:
|
||||
try:
|
||||
root_tk.after(cls.update_loop_interval, cls.update)
|
||||
app.after(cls.update_loop_interval, cls.update)
|
||||
return
|
||||
except Exception:
|
||||
continue
|
||||
|
Binary file not shown.
BIN
customtkinter/assets/fonts/CustomTkinter_shapes_font.otf
Normal file
BIN
customtkinter/assets/fonts/CustomTkinter_shapes_font.otf
Normal file
Binary file not shown.
@ -1,32 +1,39 @@
|
||||
{
|
||||
"color": {
|
||||
"window_bg_color": ["gray95", "gray12"],
|
||||
"button":["#5B97D3", "#3373B8"],
|
||||
"button_hover": ["#4A7BAD", "#1D538D"],
|
||||
"button_border": ["gray40", "#D5D9DE"],
|
||||
"checkbox_border": ["gray40", "#D5D9DE"],
|
||||
"window_bg_color": ["#EBEBEC", "#212325"],
|
||||
"button": ["#3B8ED0", "#1F6AA5"],
|
||||
"button_hover": ["#36719F", "#144870"],
|
||||
"button_border": ["#3E454A", "#949A9F"],
|
||||
"checkbox_border": ["#3E454A", "#949A9F"],
|
||||
"checkmark": ["white", "gray90"],
|
||||
"entry": ["white", "gray24"],
|
||||
"entry_border": ["gray70", "gray32"],
|
||||
"entry": ["#F9F9FA", "#343638"],
|
||||
"entry_border": ["#979DA2", "#565B5E"],
|
||||
"entry_placeholder_text": ["gray52", "gray62"],
|
||||
"frame_border": ["#A7C2E0", "#5FB4DD"],
|
||||
"frame_low": ["#E3E4E5", "gray16"],
|
||||
"frame_high": ["#D7D8D9", "gray22"],
|
||||
"frame_border": ["#979DA2", "#1F2122"],
|
||||
"frame_low": ["#D1D5D8", "#2A2D2E"],
|
||||
"frame_high": ["#D7D8D9", "#343638"],
|
||||
"label": [null, null],
|
||||
"text": ["gray20", "#D5D9DE"],
|
||||
"text": ["gray10", "#DCE4EE"],
|
||||
"text_disabled": ["gray60", "#777B80"],
|
||||
"text_button_disabled": ["gray40", "gray74"],
|
||||
"progressbar": ["#6B6B6B", "gray0"],
|
||||
"progressbar_progress": ["#5B97D3", "#3373B8"],
|
||||
"progressbar": ["#939BA2", "#4A4D50"],
|
||||
"progressbar_progress": ["#3B8ED0", "#1F6AA5"],
|
||||
"progressbar_border": ["gray", "gray"],
|
||||
"slider": ["#6B6B6B", "gray0"],
|
||||
"slider_progress": ["white", "gray40"],
|
||||
"slider_button": ["#5B97D3", "#3373B8"],
|
||||
"slider_button_hover": ["#4A7BAD", "#1D538D"],
|
||||
"switch": ["gray70", "gray35"],
|
||||
"switch_progress": ["#5B97D3", "#3373B8"],
|
||||
"slider": ["#939BA2", "#4A4D50"],
|
||||
"slider_progress": ["white", "#AAB0B5"],
|
||||
"slider_button": ["#3B8ED0", "#1F6AA5"],
|
||||
"slider_button_hover": ["#36719F", "#144870"],
|
||||
"switch": ["#939BA2", "#4A4D50"],
|
||||
"switch_progress": ["#3B8ED0", "#1F6AA5"],
|
||||
"switch_button": ["gray36", "#D5D9DE"],
|
||||
"switch_button_hover": ["gray20", "gray100"]
|
||||
"switch_button_hover": ["gray20", "gray100"],
|
||||
"optionmenu_button": ["#36719F", "#144870"],
|
||||
"optionmenu_button_hover": ["#27577D", "#203A4F"],
|
||||
"combobox_border": ["#979DA2", "#565B5E"],
|
||||
"combobox_button_hover": ["#6E7174", "#7A848D"],
|
||||
"dropdown_color": ["#A8ACB1", "#535353"],
|
||||
"dropdown_hover": ["#D6DCE2", "#393D40"],
|
||||
"dropdown_text": ["gray10", "#DCE4EE"]
|
||||
},
|
||||
"text": {
|
||||
"macOS": {
|
||||
@ -43,21 +50,21 @@
|
||||
}
|
||||
},
|
||||
"shape": {
|
||||
"button_corner_radius": 8,
|
||||
"button_corner_radius": 6,
|
||||
"button_border_width": 0,
|
||||
"checkbox_corner_radius": 7,
|
||||
"checkbox_corner_radius": 6,
|
||||
"checkbox_border_width": 3,
|
||||
"radiobutton_corner_radius": 1000,
|
||||
"radiobutton_border_width_unchecked": 3,
|
||||
"radiobutton_border_width_checked": 6,
|
||||
"entry_border_width": 2,
|
||||
"frame_corner_radius": 8,
|
||||
"frame_corner_radius": 6,
|
||||
"frame_border_width": 0,
|
||||
"label_corner_radius": 8,
|
||||
"progressbar_border_width": 0,
|
||||
"progressbar_corner_radius": 1000,
|
||||
"slider_border_width": 6,
|
||||
"slider_corner_radius": 8,
|
||||
"slider_corner_radius": 1000,
|
||||
"slider_button_length": 0,
|
||||
"slider_button_corner_radius": 1000,
|
||||
"switch_border_width": 3,
|
||||
|
@ -27,7 +27,13 @@
|
||||
"switch_progress": ["#608BD5", "#395E9C"],
|
||||
"switch_button": ["gray38", "gray70"],
|
||||
"switch_button_hover": ["gray30", "gray90"],
|
||||
"darken_factor": 0.8
|
||||
"optionmenu_button": ["#36719F", "#144870"],
|
||||
"optionmenu_button_hover": ["#27577D", "#203A4F"],
|
||||
"combobox_border": ["gray70", "gray32"],
|
||||
"combobox_button_hover": ["#6E7174", "#7A848D"],
|
||||
"dropdown_color": ["#A8ACB1", "#535353"],
|
||||
"dropdown_hover": ["#D6DCE2", "#393D40"],
|
||||
"dropdown_text": ["gray12", "gray90"]
|
||||
},
|
||||
"text": {
|
||||
"macOS": {
|
||||
|
@ -27,7 +27,13 @@
|
||||
"switch_progress": ["#72CF9F", "#11B384"],
|
||||
"switch_button": ["gray38", "gray70"],
|
||||
"switch_button_hover": ["gray30", "gray90"],
|
||||
"darken_factor": 0.8
|
||||
"optionmenu_button": ["#0E9670", "#0D8A66"],
|
||||
"optionmenu_button_hover":["gray40", "gray70"],
|
||||
"combobox_border": ["gray70", "gray32"],
|
||||
"combobox_button_hover": ["#6E7174", "#7A848D"],
|
||||
"dropdown_color": ["#A8ACB1", "#535353"],
|
||||
"dropdown_hover": ["#D6DCE2", "#393D40"],
|
||||
"dropdown_text": ["gray12", "gray90"]
|
||||
},
|
||||
"text": {
|
||||
"macOS": {
|
||||
@ -66,4 +72,4 @@
|
||||
"switch_button_corner_radius": 1000,
|
||||
"switch_button_length": 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,9 +17,11 @@ class DrawEngine:
|
||||
|
||||
Functions:
|
||||
- draw_rounded_rect_with_border()
|
||||
- draw_rounded_rect_with_border_vertical_split()
|
||||
- draw_rounded_progress_bar_with_border()
|
||||
- draw_rounded_slider_with_border_and_button()
|
||||
- draw_checkmark()
|
||||
- draw_dropdown_arrow()
|
||||
|
||||
"""
|
||||
|
||||
@ -27,9 +29,8 @@ class DrawEngine:
|
||||
|
||||
def __init__(self, canvas: CTkCanvas):
|
||||
self._canvas = canvas
|
||||
self._existing_tags = set()
|
||||
|
||||
def _calc_optimal_corner_radius(self, user_corner_radius: Union[float, int]) -> Union[float, int]:
|
||||
def __calc_optimal_corner_radius(self, user_corner_radius: Union[float, int]) -> Union[float, int]:
|
||||
# optimize for drawing with polygon shapes
|
||||
if self.preferred_drawing_method == "polygon_shapes":
|
||||
if sys.platform == "darwin":
|
||||
@ -53,13 +54,14 @@ class DrawEngine:
|
||||
else:
|
||||
return user_corner_radius
|
||||
|
||||
def draw_rounded_rect_with_border(self, width: int, height: int, corner_radius: Union[float, int], border_width: Union[float, int]) -> bool:
|
||||
def draw_rounded_rect_with_border(self, width: Union[float, int], height: Union[float, int], corner_radius: Union[float, int],
|
||||
border_width: Union[float, int], overwrite_preferred_drawing_method: str = None) -> bool:
|
||||
""" Draws a rounded rectangle with a corner_radius and border_width on the canvas. The border elements have a 'border_parts' tag,
|
||||
the main foreground elements have an 'inner_parts' tag to color the elements accordingly.
|
||||
|
||||
returns bool if recoloring is necessary """
|
||||
|
||||
width = math.floor(width / 2) * 2 # round (floor) current_width and current_height and restrict them to even values only
|
||||
width = math.floor(width / 2) * 2 # round (floor) _current_width and _current_height and restrict them to even values only
|
||||
height = math.floor(height / 2) * 2
|
||||
corner_radius = round(corner_radius)
|
||||
|
||||
@ -67,21 +69,26 @@ class DrawEngine:
|
||||
corner_radius = min(width / 2, height / 2)
|
||||
|
||||
border_width = round(border_width)
|
||||
corner_radius = self._calc_optimal_corner_radius(corner_radius) # optimize corner_radius for different drawing methods (different rounding)
|
||||
corner_radius = self.__calc_optimal_corner_radius(corner_radius) # optimize corner_radius for different drawing methods (different rounding)
|
||||
|
||||
if corner_radius >= border_width:
|
||||
inner_corner_radius = corner_radius - border_width
|
||||
else:
|
||||
inner_corner_radius = 0
|
||||
|
||||
if self.preferred_drawing_method == "polygon_shapes":
|
||||
return self._draw_rounded_rect_with_border_polygon_shapes(width, height, corner_radius, border_width, inner_corner_radius)
|
||||
elif self.preferred_drawing_method == "font_shapes":
|
||||
return self._draw_rounded_rect_with_border_font_shapes(width, height, corner_radius, border_width, inner_corner_radius, ())
|
||||
elif self.preferred_drawing_method == "circle_shapes":
|
||||
return self._draw_rounded_rect_with_border_circle_shapes(width, height, corner_radius, border_width, inner_corner_radius)
|
||||
if overwrite_preferred_drawing_method is not None:
|
||||
preferred_drawing_method = overwrite_preferred_drawing_method
|
||||
else:
|
||||
preferred_drawing_method = self.preferred_drawing_method
|
||||
|
||||
def _draw_rounded_rect_with_border_polygon_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int) -> bool:
|
||||
if preferred_drawing_method == "polygon_shapes":
|
||||
return self.__draw_rounded_rect_with_border_polygon_shapes(width, height, corner_radius, border_width, inner_corner_radius)
|
||||
elif preferred_drawing_method == "font_shapes":
|
||||
return self.__draw_rounded_rect_with_border_font_shapes(width, height, corner_radius, border_width, inner_corner_radius, ())
|
||||
elif preferred_drawing_method == "circle_shapes":
|
||||
return self.__draw_rounded_rect_with_border_circle_shapes(width, height, corner_radius, border_width, inner_corner_radius)
|
||||
|
||||
def __draw_rounded_rect_with_border_polygon_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int) -> bool:
|
||||
requires_recoloring = False
|
||||
|
||||
# create border button parts (only if border exists)
|
||||
@ -134,8 +141,8 @@ class DrawEngine:
|
||||
|
||||
return requires_recoloring
|
||||
|
||||
def _draw_rounded_rect_with_border_font_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int,
|
||||
exclude_parts: tuple) -> bool:
|
||||
def __draw_rounded_rect_with_border_font_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int,
|
||||
exclude_parts: tuple) -> bool:
|
||||
requires_recoloring = False
|
||||
|
||||
# create border button parts
|
||||
@ -272,7 +279,7 @@ class DrawEngine:
|
||||
|
||||
return requires_recoloring
|
||||
|
||||
def _draw_rounded_rect_with_border_circle_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int) -> bool:
|
||||
def __draw_rounded_rect_with_border_circle_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int) -> bool:
|
||||
requires_recoloring = False
|
||||
|
||||
# border button parts
|
||||
@ -346,22 +353,302 @@ class DrawEngine:
|
||||
|
||||
return requires_recoloring
|
||||
|
||||
def draw_rounded_progress_bar_with_border(self, width: int, height: int, corner_radius: Union[float, int], border_width: Union[float, int],
|
||||
progress_value: float, orientation: str) -> bool:
|
||||
def draw_rounded_rect_with_border_vertical_split(self, width: Union[float, int], height: Union[float, int], corner_radius: Union[float, int],
|
||||
border_width: Union[float, int], left_section_width: Union[float, int]) -> bool:
|
||||
""" Draws a rounded rectangle with a corner_radius and border_width on the canvas which is split at left_section_width.
|
||||
The border elements have the tags 'border_parts_left', 'border_parts_lright',
|
||||
the main foreground elements have an 'inner_parts_left' and inner_parts_right' tag,
|
||||
to color the elements accordingly.
|
||||
|
||||
returns bool if recoloring is necessary """
|
||||
|
||||
left_section_width = round(left_section_width)
|
||||
width = math.floor(width / 2) * 2 # round (floor) _current_width and _current_height and restrict them to even values only
|
||||
height = math.floor(height / 2) * 2
|
||||
corner_radius = round(corner_radius)
|
||||
|
||||
if corner_radius > width / 2 or corner_radius > height / 2: # restrict corner_radius if it's too larger
|
||||
corner_radius = min(width / 2, height / 2)
|
||||
|
||||
border_width = round(border_width)
|
||||
corner_radius = self.__calc_optimal_corner_radius(corner_radius) # optimize corner_radius for different drawing methods (different rounding)
|
||||
|
||||
if corner_radius >= border_width:
|
||||
inner_corner_radius = corner_radius - border_width
|
||||
else:
|
||||
inner_corner_radius = 0
|
||||
|
||||
if left_section_width > width - corner_radius * 2:
|
||||
left_section_width = width - corner_radius * 2
|
||||
elif left_section_width < corner_radius * 2:
|
||||
left_section_width = corner_radius * 2
|
||||
|
||||
if self.preferred_drawing_method == "polygon_shapes" or self.preferred_drawing_method == "circle_shapes":
|
||||
return self.__draw_rounded_rect_with_border_vertical_split_polygon_shapes(width, height, corner_radius, border_width, inner_corner_radius, left_section_width)
|
||||
elif self.preferred_drawing_method == "font_shapes":
|
||||
return self.__draw_rounded_rect_with_border_vertical_split_font_shapes(width, height, corner_radius, border_width, inner_corner_radius, left_section_width, ())
|
||||
|
||||
def __draw_rounded_rect_with_border_vertical_split_polygon_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int,
|
||||
left_section_width: int) -> bool:
|
||||
requires_recoloring = False
|
||||
|
||||
# create border button parts (only if border exists)
|
||||
if border_width > 0:
|
||||
if not self._canvas.find_withtag("border_parts"):
|
||||
self._canvas.create_polygon((0, 0, 0, 0), tags=("border_line_left_1", "border_parts_left", "border_parts", "left_parts"))
|
||||
self._canvas.create_polygon((0, 0, 0, 0), tags=("border_line_right_1", "border_parts_right", "border_parts", "right_parts"))
|
||||
self._canvas.create_rectangle((0, 0, 0, 0), tags=("border_rect_left_1", "border_parts_left", "border_parts", "left_parts"))
|
||||
self._canvas.create_rectangle((0, 0, 0, 0), tags=("border_rect_right_1", "border_parts_right", "border_parts", "right_parts"))
|
||||
requires_recoloring = True
|
||||
|
||||
self._canvas.coords("border_line_left_1",
|
||||
(corner_radius,
|
||||
corner_radius,
|
||||
left_section_width - corner_radius,
|
||||
corner_radius,
|
||||
left_section_width - corner_radius,
|
||||
height - corner_radius,
|
||||
corner_radius,
|
||||
height - corner_radius))
|
||||
self._canvas.coords("border_line_right_1",
|
||||
(left_section_width + corner_radius,
|
||||
corner_radius,
|
||||
width - corner_radius,
|
||||
corner_radius,
|
||||
width - corner_radius,
|
||||
height - corner_radius,
|
||||
left_section_width + corner_radius,
|
||||
height - corner_radius))
|
||||
self._canvas.coords("border_rect_left_1",
|
||||
(left_section_width - corner_radius,
|
||||
0,
|
||||
left_section_width,
|
||||
height))
|
||||
self._canvas.coords("border_rect_right_1",
|
||||
(left_section_width,
|
||||
0,
|
||||
left_section_width + corner_radius,
|
||||
height))
|
||||
self._canvas.itemconfig("border_line_left_1", joinstyle=tkinter.ROUND, width=corner_radius * 2)
|
||||
self._canvas.itemconfig("border_line_right_1", joinstyle=tkinter.ROUND, width=corner_radius * 2)
|
||||
|
||||
else:
|
||||
self._canvas.delete("border_parts")
|
||||
|
||||
# create inner button parts
|
||||
if not self._canvas.find_withtag("inner_parts"):
|
||||
self._canvas.create_polygon((0, 0, 0, 0), tags=("inner_line_left_1", "inner_parts_left", "inner_parts", "left_parts"), joinstyle=tkinter.ROUND)
|
||||
self._canvas.create_polygon((0, 0, 0, 0), tags=("inner_line_right_1", "inner_parts_right", "inner_parts", "right_parts"), joinstyle=tkinter.ROUND)
|
||||
self._canvas.create_rectangle((0, 0, 0, 0), tags=("inner_rect_left_1", "inner_parts_left", "inner_parts", "left_parts"), width=0)
|
||||
self._canvas.create_rectangle((0, 0, 0, 0), tags=("inner_rect_right_1", "inner_parts_right", "inner_parts", "right_parts"), width=0)
|
||||
requires_recoloring = True
|
||||
|
||||
self._canvas.coords("inner_line_left_1",
|
||||
corner_radius,
|
||||
corner_radius,
|
||||
left_section_width - inner_corner_radius,
|
||||
corner_radius,
|
||||
left_section_width - inner_corner_radius,
|
||||
height - corner_radius,
|
||||
corner_radius,
|
||||
height - corner_radius)
|
||||
self._canvas.coords("inner_line_right_1",
|
||||
left_section_width + inner_corner_radius,
|
||||
corner_radius,
|
||||
width - corner_radius,
|
||||
corner_radius,
|
||||
width - corner_radius,
|
||||
height - corner_radius,
|
||||
left_section_width + inner_corner_radius,
|
||||
height - corner_radius)
|
||||
self._canvas.coords("inner_rect_left_1",
|
||||
(left_section_width - inner_corner_radius,
|
||||
border_width,
|
||||
left_section_width,
|
||||
height - border_width))
|
||||
self._canvas.coords("inner_rect_right_1",
|
||||
(left_section_width,
|
||||
border_width,
|
||||
left_section_width + inner_corner_radius,
|
||||
height - border_width))
|
||||
self._canvas.itemconfig("inner_line_left_1", width=inner_corner_radius * 2)
|
||||
self._canvas.itemconfig("inner_line_right_1", width=inner_corner_radius * 2)
|
||||
|
||||
if requires_recoloring: # new parts were added -> manage z-order
|
||||
self._canvas.tag_lower("inner_parts")
|
||||
self._canvas.tag_lower("border_parts")
|
||||
|
||||
return requires_recoloring
|
||||
|
||||
def __draw_rounded_rect_with_border_vertical_split_font_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int,
|
||||
left_section_width: int, exclude_parts: tuple) -> bool:
|
||||
requires_recoloring = False
|
||||
|
||||
# create border button parts
|
||||
if border_width > 0:
|
||||
if corner_radius > 0:
|
||||
# create canvas border corner parts if not already created, but only if needed, and delete if not needed
|
||||
if not self._canvas.find_withtag("border_oval_1_a") and "border_oval_1" not in exclude_parts:
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_1_a", "border_corner_part", "border_parts_left", "border_parts", "left_parts"), anchor=tkinter.CENTER)
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_1_b", "border_corner_part", "border_parts_left", "border_parts", "left_parts"), anchor=tkinter.CENTER, angle=180)
|
||||
requires_recoloring = True
|
||||
elif self._canvas.find_withtag("border_oval_1_a") and "border_oval_1" in exclude_parts:
|
||||
self._canvas.delete("border_oval_1_a", "border_oval_1_b")
|
||||
|
||||
if not self._canvas.find_withtag("border_oval_2_a") and width > 2 * corner_radius and "border_oval_2" not in exclude_parts:
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_2_a", "border_corner_part", "border_parts_right", "border_parts", "right_parts"), anchor=tkinter.CENTER)
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_2_b", "border_corner_part", "border_parts_right", "border_parts", "right_parts"), anchor=tkinter.CENTER, angle=180)
|
||||
requires_recoloring = True
|
||||
elif self._canvas.find_withtag("border_oval_2_a") and (not width > 2 * corner_radius or "border_oval_2" in exclude_parts):
|
||||
self._canvas.delete("border_oval_2_a", "border_oval_2_b")
|
||||
|
||||
if not self._canvas.find_withtag("border_oval_3_a") and height > 2 * corner_radius \
|
||||
and width > 2 * corner_radius and "border_oval_3" not in exclude_parts:
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_3_a", "border_corner_part", "border_parts_right", "border_parts", "right_parts"), anchor=tkinter.CENTER)
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_3_b", "border_corner_part", "border_parts_right", "border_parts", "right_parts"), anchor=tkinter.CENTER, angle=180)
|
||||
requires_recoloring = True
|
||||
elif self._canvas.find_withtag("border_oval_3_a") and (not (height > 2 * corner_radius
|
||||
and width > 2 * corner_radius) or "border_oval_3" in exclude_parts):
|
||||
self._canvas.delete("border_oval_3_a", "border_oval_3_b")
|
||||
|
||||
if not self._canvas.find_withtag("border_oval_4_a") and height > 2 * corner_radius and "border_oval_4" not in exclude_parts:
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_4_a", "border_corner_part", "border_parts_left", "border_parts", "left_parts"), anchor=tkinter.CENTER)
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_4_b", "border_corner_part", "border_parts_left", "border_parts", "left_parts"), anchor=tkinter.CENTER, angle=180)
|
||||
requires_recoloring = True
|
||||
elif self._canvas.find_withtag("border_oval_4_a") and (not height > 2 * corner_radius or "border_oval_4" in exclude_parts):
|
||||
self._canvas.delete("border_oval_4_a", "border_oval_4_b")
|
||||
|
||||
# change position of border corner parts
|
||||
self._canvas.coords("border_oval_1_a", corner_radius, corner_radius, corner_radius)
|
||||
self._canvas.coords("border_oval_1_b", corner_radius, corner_radius, corner_radius)
|
||||
self._canvas.coords("border_oval_2_a", width - corner_radius, corner_radius, corner_radius)
|
||||
self._canvas.coords("border_oval_2_b", width - corner_radius, corner_radius, corner_radius)
|
||||
self._canvas.coords("border_oval_3_a", width - corner_radius, height - corner_radius, corner_radius)
|
||||
self._canvas.coords("border_oval_3_b", width - corner_radius, height - corner_radius, corner_radius)
|
||||
self._canvas.coords("border_oval_4_a", corner_radius, height - corner_radius, corner_radius)
|
||||
self._canvas.coords("border_oval_4_b", corner_radius, height - corner_radius, corner_radius)
|
||||
|
||||
else:
|
||||
self._canvas.delete("border_corner_part") # delete border corner parts if not needed
|
||||
|
||||
# create canvas border rectangle parts if not already created
|
||||
if not self._canvas.find_withtag("border_rectangle_1"):
|
||||
self._canvas.create_rectangle(0, 0, 0, 0, tags=("border_rectangle_left_1", "border_rectangle_part", "border_parts_left", "border_parts", "left_parts"), width=0)
|
||||
self._canvas.create_rectangle(0, 0, 0, 0, tags=("border_rectangle_left_2", "border_rectangle_part", "border_parts_left", "border_parts", "left_parts"), width=0)
|
||||
self._canvas.create_rectangle(0, 0, 0, 0, tags=("border_rectangle_right_1", "border_rectangle_part", "border_parts_right", "border_parts", "right_parts"), width=0)
|
||||
self._canvas.create_rectangle(0, 0, 0, 0, tags=("border_rectangle_right_2", "border_rectangle_part", "border_parts_right", "border_parts", "right_parts"), width=0)
|
||||
requires_recoloring = True
|
||||
|
||||
# change position of border rectangle parts
|
||||
self._canvas.coords("border_rectangle_left_1", (0, corner_radius, left_section_width, height - corner_radius))
|
||||
self._canvas.coords("border_rectangle_left_2", (corner_radius, 0, left_section_width, height))
|
||||
self._canvas.coords("border_rectangle_right_1", (left_section_width, corner_radius, width, height - corner_radius))
|
||||
self._canvas.coords("border_rectangle_right_2", (left_section_width, 0, width - corner_radius, height))
|
||||
|
||||
else:
|
||||
self._canvas.delete("border_parts")
|
||||
|
||||
# create inner button parts
|
||||
if inner_corner_radius > 0:
|
||||
|
||||
# create canvas border corner parts if not already created, but only if they're needed and delete if not needed
|
||||
if not self._canvas.find_withtag("inner_oval_1_a") and "inner_oval_1" not in exclude_parts:
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_1_a", "inner_corner_part", "inner_parts_left", "inner_parts", "left_parts"), anchor=tkinter.CENTER)
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_1_b", "inner_corner_part", "inner_parts_left", "inner_parts", "left_parts"), anchor=tkinter.CENTER, angle=180)
|
||||
requires_recoloring = True
|
||||
elif self._canvas.find_withtag("inner_oval_1_a") and "inner_oval_1" in exclude_parts:
|
||||
self._canvas.delete("inner_oval_1_a", "inner_oval_1_b")
|
||||
|
||||
if not self._canvas.find_withtag("inner_oval_2_a") and width - (2 * border_width) > 2 * inner_corner_radius and "inner_oval_2" not in exclude_parts:
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_2_a", "inner_corner_part","inner_parts_right", "inner_parts", "right_parts"), anchor=tkinter.CENTER)
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_2_b", "inner_corner_part", "inner_parts_right", "inner_parts", "right_parts"), anchor=tkinter.CENTER, angle=180)
|
||||
requires_recoloring = True
|
||||
elif self._canvas.find_withtag("inner_oval_2_a") and (not width - (2 * border_width) > 2 * inner_corner_radius or "inner_oval_2" in exclude_parts):
|
||||
self._canvas.delete("inner_oval_2_a", "inner_oval_2_b")
|
||||
|
||||
if not self._canvas.find_withtag("inner_oval_3_a") and height - (2 * border_width) > 2 * inner_corner_radius \
|
||||
and width - (2 * border_width) > 2 * inner_corner_radius and "inner_oval_3" not in exclude_parts:
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_3_a", "inner_corner_part", "inner_parts_right", "inner_parts", "right_parts"), anchor=tkinter.CENTER)
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_3_b", "inner_corner_part", "inner_parts_right", "inner_parts", "right_parts"), anchor=tkinter.CENTER, angle=180)
|
||||
requires_recoloring = True
|
||||
elif self._canvas.find_withtag("inner_oval_3_a") and (not (height - (2 * border_width) > 2 * inner_corner_radius
|
||||
and width - (2 * border_width) > 2 * inner_corner_radius) or "inner_oval_3" in exclude_parts):
|
||||
self._canvas.delete("inner_oval_3_a", "inner_oval_3_b")
|
||||
|
||||
if not self._canvas.find_withtag("inner_oval_4_a") and height - (2 * border_width) > 2 * inner_corner_radius and "inner_oval_4" not in exclude_parts:
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_4_a", "inner_corner_part", "inner_parts_left", "inner_parts", "left_parts"), anchor=tkinter.CENTER)
|
||||
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_4_b", "inner_corner_part", "inner_parts_left", "inner_parts", "left_parts"), anchor=tkinter.CENTER, angle=180)
|
||||
requires_recoloring = True
|
||||
elif self._canvas.find_withtag("inner_oval_4_a") and (not height - (2 * border_width) > 2 * inner_corner_radius or "inner_oval_4" in exclude_parts):
|
||||
self._canvas.delete("inner_oval_4_a", "inner_oval_4_b")
|
||||
|
||||
# change position of border corner parts
|
||||
self._canvas.coords("inner_oval_1_a", border_width + inner_corner_radius, border_width + inner_corner_radius, inner_corner_radius)
|
||||
self._canvas.coords("inner_oval_1_b", border_width + inner_corner_radius, border_width + inner_corner_radius, inner_corner_radius)
|
||||
self._canvas.coords("inner_oval_2_a", width - border_width - inner_corner_radius, border_width + inner_corner_radius, inner_corner_radius)
|
||||
self._canvas.coords("inner_oval_2_b", width - border_width - inner_corner_radius, border_width + inner_corner_radius, inner_corner_radius)
|
||||
self._canvas.coords("inner_oval_3_a", width - border_width - inner_corner_radius, height - border_width - inner_corner_radius, inner_corner_radius)
|
||||
self._canvas.coords("inner_oval_3_b", width - border_width - inner_corner_radius, height - border_width - inner_corner_radius, inner_corner_radius)
|
||||
self._canvas.coords("inner_oval_4_a", border_width + inner_corner_radius, height - border_width - inner_corner_radius, inner_corner_radius)
|
||||
self._canvas.coords("inner_oval_4_b", border_width + inner_corner_radius, height - border_width - inner_corner_radius, inner_corner_radius)
|
||||
else:
|
||||
self._canvas.delete("inner_corner_part") # delete inner corner parts if not needed
|
||||
|
||||
# create canvas inner rectangle parts if not already created
|
||||
if not self._canvas.find_withtag("inner_rectangle_1"):
|
||||
self._canvas.create_rectangle(0, 0, 0, 0, tags=("inner_rectangle_left_1", "inner_rectangle_part", "inner_parts_left", "inner_parts", "left_parts"), width=0)
|
||||
self._canvas.create_rectangle(0, 0, 0, 0, tags=("inner_rectangle_right_1", "inner_rectangle_part", "inner_parts_right", "inner_parts", "right_parts"), width=0)
|
||||
requires_recoloring = True
|
||||
|
||||
if not self._canvas.find_withtag("inner_rectangle_2") and inner_corner_radius * 2 < height - (border_width * 2):
|
||||
self._canvas.create_rectangle(0, 0, 0, 0, tags=("inner_rectangle_left_2", "inner_rectangle_part", "inner_parts_left", "inner_parts", "left_parts"), width=0)
|
||||
self._canvas.create_rectangle(0, 0, 0, 0, tags=("inner_rectangle_right_2", "inner_rectangle_part", "inner_parts_right", "inner_parts", "right_parts"), width=0)
|
||||
requires_recoloring = True
|
||||
|
||||
elif self._canvas.find_withtag("inner_rectangle_2") and not inner_corner_radius * 2 < height - (border_width * 2):
|
||||
self._canvas.delete("inner_rectangle_left_2")
|
||||
self._canvas.delete("inner_rectangle_right_2")
|
||||
|
||||
# change position of inner rectangle parts
|
||||
self._canvas.coords("inner_rectangle_left_1", (border_width + inner_corner_radius,
|
||||
border_width,
|
||||
left_section_width,
|
||||
height - border_width))
|
||||
self._canvas.coords("inner_rectangle_left_2", (border_width,
|
||||
border_width + inner_corner_radius,
|
||||
left_section_width,
|
||||
height - inner_corner_radius - border_width))
|
||||
self._canvas.coords("inner_rectangle_right_1", (left_section_width,
|
||||
border_width,
|
||||
width - border_width - inner_corner_radius,
|
||||
height - border_width))
|
||||
self._canvas.coords("inner_rectangle_right_2", (left_section_width,
|
||||
border_width + inner_corner_radius,
|
||||
width - border_width,
|
||||
height - inner_corner_radius - border_width))
|
||||
|
||||
if requires_recoloring: # new parts were added -> manage z-order
|
||||
self._canvas.tag_lower("inner_parts")
|
||||
self._canvas.tag_lower("border_parts")
|
||||
|
||||
return requires_recoloring
|
||||
|
||||
def draw_rounded_progress_bar_with_border(self, width: Union[float, int], height: Union[float, int], corner_radius: Union[float, int],
|
||||
border_width: Union[float, int], progress_value: float, orientation: str) -> bool:
|
||||
""" Draws a rounded bar on the canvas, which is split in half according to the argument 'progress_value' (0 - 1).
|
||||
The border elements get the 'border_parts' tag", the main elements get the 'inner_parts' tag and
|
||||
the progress elements get the 'progress_parts' tag. The 'orientation' argument defines from which direction the progress starts (n, w, s, e).
|
||||
|
||||
returns bool if recoloring is necessary """
|
||||
|
||||
width = math.floor(width / 2) * 2 # round current_width and current_height and restrict them to even values only
|
||||
width = math.floor(width / 2) * 2 # round _current_width and _current_height and restrict them to even values only
|
||||
height = math.floor(height / 2) * 2
|
||||
|
||||
if corner_radius > width / 2 or corner_radius > height / 2: # restrict corner_radius if it's too larger
|
||||
corner_radius = min(width / 2, height / 2)
|
||||
|
||||
border_width = round(border_width)
|
||||
corner_radius = self._calc_optimal_corner_radius(corner_radius) # optimize corner_radius for different drawing methods (different rounding)
|
||||
corner_radius = self.__calc_optimal_corner_radius(corner_radius) # optimize corner_radius for different drawing methods (different rounding)
|
||||
|
||||
if corner_radius >= border_width:
|
||||
inner_corner_radius = corner_radius - border_width
|
||||
@ -369,16 +656,16 @@ class DrawEngine:
|
||||
inner_corner_radius = 0
|
||||
|
||||
if self.preferred_drawing_method == "polygon_shapes" or self.preferred_drawing_method == "circle_shapes":
|
||||
return self._draw_rounded_progress_bar_with_border_polygon_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
progress_value, orientation)
|
||||
return self.__draw_rounded_progress_bar_with_border_polygon_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
progress_value, orientation)
|
||||
elif self.preferred_drawing_method == "font_shapes":
|
||||
return self._draw_rounded_progress_bar_with_border_font_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
progress_value, orientation)
|
||||
return self.__draw_rounded_progress_bar_with_border_font_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
progress_value, orientation)
|
||||
|
||||
def _draw_rounded_progress_bar_with_border_polygon_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int,
|
||||
progress_value: float, orientation: str) -> bool:
|
||||
def __draw_rounded_progress_bar_with_border_polygon_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int,
|
||||
progress_value: float, orientation: str) -> bool:
|
||||
|
||||
requires_recoloring = self._draw_rounded_rect_with_border_polygon_shapes(width, height, corner_radius, border_width, inner_corner_radius)
|
||||
requires_recoloring = self.__draw_rounded_rect_with_border_polygon_shapes(width, height, corner_radius, border_width, inner_corner_radius)
|
||||
|
||||
if corner_radius <= border_width:
|
||||
bottom_right_shift = 0 # weird canvas rendering inaccuracy that has to be corrected in some cases
|
||||
@ -417,8 +704,8 @@ class DrawEngine:
|
||||
|
||||
return requires_recoloring
|
||||
|
||||
def _draw_rounded_progress_bar_with_border_font_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int,
|
||||
progress_value: float, orientation: str) -> bool:
|
||||
def __draw_rounded_progress_bar_with_border_font_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int,
|
||||
progress_value: float, orientation: str) -> bool:
|
||||
|
||||
requires_recoloring, requires_recoloring_2 = False, False
|
||||
|
||||
@ -452,8 +739,8 @@ class DrawEngine:
|
||||
|
||||
# horizontal orientation from the bottom
|
||||
if orientation == "w":
|
||||
requires_recoloring_2 = self._draw_rounded_rect_with_border_font_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
("inner_oval_1", "inner_oval_4"))
|
||||
requires_recoloring_2 = self.__draw_rounded_rect_with_border_font_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
("inner_oval_1", "inner_oval_4"))
|
||||
|
||||
# set positions of progress corner parts
|
||||
self._canvas.coords("progress_oval_1_a", border_width + inner_corner_radius, border_width + inner_corner_radius, inner_corner_radius)
|
||||
@ -483,8 +770,8 @@ class DrawEngine:
|
||||
|
||||
# vertical orientation from the bottom
|
||||
if orientation == "s":
|
||||
requires_recoloring_2 = self._draw_rounded_rect_with_border_font_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
("inner_oval_3", "inner_oval_4"))
|
||||
requires_recoloring_2 = self.__draw_rounded_rect_with_border_font_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
("inner_oval_3", "inner_oval_4"))
|
||||
|
||||
# set positions of progress corner parts
|
||||
self._canvas.coords("progress_oval_1_a", border_width + inner_corner_radius,
|
||||
@ -514,11 +801,11 @@ class DrawEngine:
|
||||
|
||||
return requires_recoloring or requires_recoloring_2
|
||||
|
||||
def draw_rounded_slider_with_border_and_button(self, width: int, height: int, corner_radius: Union[float, int], border_width: Union[float, int],
|
||||
button_length: Union[float, int], button_corner_radius: Union[float, int], slider_value: float,
|
||||
orientation: str) -> bool:
|
||||
def draw_rounded_slider_with_border_and_button(self, width: Union[float, int], height: Union[float, int], corner_radius: Union[float, int],
|
||||
border_width: Union[float, int], button_length: Union[float, int], button_corner_radius: Union[float, int],
|
||||
slider_value: float, orientation: str) -> bool:
|
||||
|
||||
width = math.floor(width / 2) * 2 # round current_width and current_height and restrict them to even values only
|
||||
width = math.floor(width / 2) * 2 # round _current_width and _current_height and restrict them to even values only
|
||||
height = math.floor(height / 2) * 2
|
||||
|
||||
if corner_radius > width / 2 or corner_radius > height / 2: # restrict corner_radius if it's too larger
|
||||
@ -530,7 +817,7 @@ class DrawEngine:
|
||||
button_length = round(button_length)
|
||||
border_width = round(border_width)
|
||||
button_corner_radius = round(button_corner_radius)
|
||||
corner_radius = self._calc_optimal_corner_radius(corner_radius) # optimize corner_radius for different drawing methods (different rounding)
|
||||
corner_radius = self.__calc_optimal_corner_radius(corner_radius) # optimize corner_radius for different drawing methods (different rounding)
|
||||
|
||||
if corner_radius >= border_width:
|
||||
inner_corner_radius = corner_radius - border_width
|
||||
@ -538,18 +825,18 @@ class DrawEngine:
|
||||
inner_corner_radius = 0
|
||||
|
||||
if self.preferred_drawing_method == "polygon_shapes" or self.preferred_drawing_method == "circle_shapes":
|
||||
return self._draw_rounded_slider_with_border_and_button_polygon_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
button_length, button_corner_radius, slider_value, orientation)
|
||||
return self.__draw_rounded_slider_with_border_and_button_polygon_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
button_length, button_corner_radius, slider_value, orientation)
|
||||
elif self.preferred_drawing_method == "font_shapes":
|
||||
return self._draw_rounded_slider_with_border_and_button_font_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
button_length, button_corner_radius, slider_value, orientation)
|
||||
return self.__draw_rounded_slider_with_border_and_button_font_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
button_length, button_corner_radius, slider_value, orientation)
|
||||
|
||||
def _draw_rounded_slider_with_border_and_button_polygon_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int,
|
||||
button_length: int, button_corner_radius: int, slider_value: float, orientation: str) -> bool:
|
||||
def __draw_rounded_slider_with_border_and_button_polygon_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int,
|
||||
button_length: int, button_corner_radius: int, slider_value: float, orientation: str) -> bool:
|
||||
|
||||
# draw normal progressbar
|
||||
requires_recoloring = self._draw_rounded_progress_bar_with_border_polygon_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
slider_value, orientation)
|
||||
requires_recoloring = self.__draw_rounded_progress_bar_with_border_polygon_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
slider_value, orientation)
|
||||
|
||||
# create slider button part
|
||||
if not self._canvas.find_withtag("slider_parts"):
|
||||
@ -583,12 +870,12 @@ class DrawEngine:
|
||||
|
||||
return requires_recoloring
|
||||
|
||||
def _draw_rounded_slider_with_border_and_button_font_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int,
|
||||
button_length: int, button_corner_radius: int, slider_value: float, orientation: str) -> bool:
|
||||
def __draw_rounded_slider_with_border_and_button_font_shapes(self, width: int, height: int, corner_radius: int, border_width: int, inner_corner_radius: int,
|
||||
button_length: int, button_corner_radius: int, slider_value: float, orientation: str) -> bool:
|
||||
|
||||
# draw normal progressbar
|
||||
requires_recoloring = self._draw_rounded_progress_bar_with_border_font_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
slider_value, orientation)
|
||||
requires_recoloring = self.__draw_rounded_progress_bar_with_border_font_shapes(width, height, corner_radius, border_width, inner_corner_radius,
|
||||
slider_value, orientation)
|
||||
|
||||
# create 4 circles (if not needed, then less)
|
||||
if not self._canvas.find_withtag("slider_oval_1_a"):
|
||||
@ -672,7 +959,7 @@ class DrawEngine:
|
||||
|
||||
return requires_recoloring
|
||||
|
||||
def draw_checkmark(self, width: int, height: int, size: Union[int, float]) -> bool:
|
||||
def draw_checkmark(self, width: Union[float, int], height: Union[float, int], size: Union[int, float]) -> bool:
|
||||
""" Draws a rounded rectangle with a corner_radius and border_width on the canvas. The border elements have a 'border_parts' tag,
|
||||
the main foreground elements have an 'inner_parts' tag to color the elements accordingly.
|
||||
|
||||
@ -701,3 +988,35 @@ class DrawEngine:
|
||||
self._canvas.coords("checkmark", round(width / 2), round(height / 2))
|
||||
|
||||
return requires_recoloring
|
||||
|
||||
def draw_dropdown_arrow(self, x_position: Union[int, float], y_position: Union[int, float], size: Union[int, float]) -> bool:
|
||||
""" Draws a dropdown bottom facing arrow at (x_position, y_position) in a given size
|
||||
|
||||
returns bool if recoloring is necessary """
|
||||
|
||||
x_position, y_position, size = round(x_position), round(y_position), round(size)
|
||||
requires_recoloring = False
|
||||
|
||||
if self.preferred_drawing_method == "polygon_shapes" or self.preferred_drawing_method == "circle_shapes":
|
||||
if not self._canvas.find_withtag("dropdown_arrow"):
|
||||
self._canvas.create_line(0, 0, 0, 0, tags="dropdown_arrow", width=round(size / 3), joinstyle=tkinter.ROUND, capstyle=tkinter.ROUND)
|
||||
self._canvas.tag_raise("dropdown_arrow")
|
||||
requires_recoloring = True
|
||||
|
||||
self._canvas.coords("dropdown_arrow",
|
||||
x_position - (size / 2),
|
||||
y_position - (size / 5),
|
||||
x_position,
|
||||
y_position + (size / 5),
|
||||
x_position + (size / 2),
|
||||
y_position - (size / 5))
|
||||
|
||||
elif self.preferred_drawing_method == "font_shapes":
|
||||
if not self._canvas.find_withtag("dropdown_arrow"):
|
||||
self._canvas.create_text(0, 0, text="Y", font=("CustomTkinter_shapes_font", -size), tags="dropdown_arrow", anchor=tkinter.CENTER)
|
||||
self._canvas.tag_raise("dropdown_arrow")
|
||||
requires_recoloring = True
|
||||
|
||||
self._canvas.coords("dropdown_arrow", x_position, y_position)
|
||||
|
||||
return requires_recoloring
|
||||
|
@ -51,7 +51,7 @@ class FontManager:
|
||||
return cls.windows_load_font(font_path, private=True, enumerable=False)
|
||||
|
||||
# Linux
|
||||
elif sys.platform.startswith("win"):
|
||||
elif sys.platform.startswith("linux"):
|
||||
try:
|
||||
shutil.copy(font_path, os.path.expanduser("~/.fonts/"))
|
||||
return True
|
||||
|
@ -124,7 +124,7 @@ class ScalingTracker:
|
||||
|
||||
@classmethod
|
||||
def activate_high_dpi_awareness(cls):
|
||||
""" make process DPI aware, customtkinter elemets will get scaled automatically,
|
||||
""" make process DPI aware, customtkinter elements will get scaled automatically,
|
||||
only gets activated when CTk object is created """
|
||||
|
||||
if not cls.deactivate_automatic_dpi_awareness:
|
||||
@ -135,42 +135,45 @@ class ScalingTracker:
|
||||
from ctypes import windll
|
||||
windll.shcore.SetProcessDpiAwareness(2)
|
||||
# Microsoft Docs: https://docs.microsoft.com/en-us/windows/win32/api/shellscalingapi/ne-shellscalingapi-process_dpi_awareness
|
||||
|
||||
else:
|
||||
pass # DPI awareness on Linux not implemented
|
||||
|
||||
@classmethod
|
||||
def get_window_dpi_scaling(cls, window) -> float:
|
||||
if sys.platform == "darwin":
|
||||
return 1 # scaling works automatically on macOS
|
||||
if not cls.deactivate_automatic_dpi_awareness:
|
||||
if sys.platform == "darwin":
|
||||
return 1 # scaling works automatically on macOS
|
||||
|
||||
elif sys.platform.startswith("win"):
|
||||
from ctypes import windll, pointer, wintypes
|
||||
elif sys.platform.startswith("win"):
|
||||
from ctypes import windll, pointer, wintypes
|
||||
|
||||
DPI100pc = 96 # DPI 96 is 100% scaling
|
||||
DPI_type = 0 # MDT_EFFECTIVE_DPI = 0, MDT_ANGULAR_DPI = 1, MDT_RAW_DPI = 2
|
||||
window_hwnd = wintypes.HWND(window.winfo_id())
|
||||
monitor_handle = windll.user32.MonitorFromWindow(window_hwnd, wintypes.DWORD(2)) # MONITOR_DEFAULTTONEAREST = 2
|
||||
x_dpi, y_dpi = wintypes.UINT(), wintypes.UINT()
|
||||
windll.shcore.GetDpiForMonitor(monitor_handle, DPI_type, pointer(x_dpi), pointer(y_dpi))
|
||||
return (x_dpi.value + y_dpi.value) / (2 * DPI100pc)
|
||||
DPI100pc = 96 # DPI 96 is 100% scaling
|
||||
DPI_type = 0 # MDT_EFFECTIVE_DPI = 0, MDT_ANGULAR_DPI = 1, MDT_RAW_DPI = 2
|
||||
window_hwnd = wintypes.HWND(window.winfo_id())
|
||||
monitor_handle = windll.user32.MonitorFromWindow(window_hwnd, wintypes.DWORD(2)) # MONITOR_DEFAULTTONEAREST = 2
|
||||
x_dpi, y_dpi = wintypes.UINT(), wintypes.UINT()
|
||||
windll.shcore.GetDpiForMonitor(monitor_handle, DPI_type, pointer(x_dpi), pointer(y_dpi))
|
||||
return (x_dpi.value + y_dpi.value) / (2 * DPI100pc)
|
||||
|
||||
else:
|
||||
return 1 # DPI awareness on Linux not implemented
|
||||
else:
|
||||
return 1 # DPI awareness on Linux not implemented
|
||||
return 1
|
||||
|
||||
@classmethod
|
||||
def check_dpi_scaling(cls):
|
||||
# check for every window if scaling value changed
|
||||
for window in cls.window_widgets_dict:
|
||||
current_dpi_scaling_value = cls.get_window_dpi_scaling(window)
|
||||
if current_dpi_scaling_value != cls.window_dpi_scaling_dict[window]:
|
||||
cls.window_dpi_scaling_dict[window] = current_dpi_scaling_value
|
||||
cls.update_scaling_callbacks_for_window(window)
|
||||
if window.winfo_exists():
|
||||
current_dpi_scaling_value = cls.get_window_dpi_scaling(window)
|
||||
if current_dpi_scaling_value != cls.window_dpi_scaling_dict[window]:
|
||||
cls.window_dpi_scaling_dict[window] = current_dpi_scaling_value
|
||||
cls.update_scaling_callbacks_for_window(window)
|
||||
|
||||
# find an existing tkinter object for the next call of .after()
|
||||
for root_tk in cls.window_widgets_dict.keys():
|
||||
for app in cls.window_widgets_dict.keys():
|
||||
try:
|
||||
root_tk.after(cls.update_loop_interval, cls.check_dpi_scaling)
|
||||
app.after(cls.update_loop_interval, cls.check_dpi_scaling)
|
||||
return
|
||||
except Exception:
|
||||
continue
|
||||
|
@ -62,6 +62,19 @@ class ThemeManager:
|
||||
|
||||
return cls.rgb2hex(new_rgb)
|
||||
|
||||
@classmethod
|
||||
def get_minimal_darker(cls, color: str) -> str:
|
||||
if color.startswith("#"):
|
||||
color_rgb = cls.hex2rgb(color)
|
||||
if color_rgb[0] > 0:
|
||||
return cls.rgb2hex((color_rgb[0] - 1, color_rgb[1], color_rgb[2]))
|
||||
elif color_rgb[1] > 0:
|
||||
return cls.rgb2hex((color_rgb[0], color_rgb[1] - 1, color_rgb[2]))
|
||||
elif color_rgb[2] > 0:
|
||||
return cls.rgb2hex((color_rgb[0], color_rgb[1], color_rgb[2] - 1))
|
||||
else:
|
||||
return cls.rgb2hex((color_rgb[0] + 1, color_rgb[1], color_rgb[2] - 1)) # otherwise slightly lighter
|
||||
|
||||
@classmethod
|
||||
def multiply_hex_color(cls, hex_color: str, factor: float = 1.0) -> str:
|
||||
try:
|
||||
|
@ -20,8 +20,8 @@ class CTkButton(CTkBaseClass):
|
||||
border_width="default_theme",
|
||||
command=None,
|
||||
textvariable=None,
|
||||
width=120,
|
||||
height=30,
|
||||
width=140,
|
||||
height=28,
|
||||
corner_radius="default_theme",
|
||||
text_font="default_theme",
|
||||
text_color="default_theme",
|
||||
@ -33,7 +33,7 @@ class CTkButton(CTkBaseClass):
|
||||
state=tkinter.NORMAL,
|
||||
**kwargs):
|
||||
|
||||
# transfer basic functionality (bg_color, size, appearance_mode, scaling) to CTkBaseClass
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
|
||||
|
||||
self.configure_basic_grid()
|
||||
@ -66,8 +66,8 @@ class CTkButton(CTkBaseClass):
|
||||
|
||||
self.canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self.desired_width),
|
||||
height=self.apply_widget_scaling(self.desired_height))
|
||||
width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.canvas.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="nsew")
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
@ -98,34 +98,41 @@ class CTkButton(CTkBaseClass):
|
||||
self.image_label.destroy()
|
||||
self.image_label = None
|
||||
|
||||
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()
|
||||
|
||||
def set_dimensions(self, width=None, height=None):
|
||||
super().set_dimensions(width, height)
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self.current_width),
|
||||
self.apply_widget_scaling(self.current_height),
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
self.apply_widget_scaling(self.border_width))
|
||||
|
||||
if no_color_updates is False or requires_recoloring:
|
||||
|
||||
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))
|
||||
|
||||
# set color for the button border parts (outline)
|
||||
self.canvas.itemconfig("border_parts",
|
||||
outline=ThemeManager.single_color(self.border_color, self.appearance_mode),
|
||||
fill=ThemeManager.single_color(self.border_color, self.appearance_mode))
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
|
||||
# set color for inner button parts
|
||||
if self.fg_color is None:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
outline=ThemeManager.single_color(self.bg_color, self.appearance_mode),
|
||||
fill=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
fill=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
# create text label if text given
|
||||
if self.text is not None and self.text != "":
|
||||
@ -142,17 +149,17 @@ class CTkButton(CTkBaseClass):
|
||||
|
||||
if no_color_updates is False:
|
||||
# set text_label fg color (text color)
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self.appearance_mode))
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
|
||||
if self.state == tkinter.DISABLED:
|
||||
self.text_label.configure(fg=(ThemeManager.single_color(self.text_color_disabled, self.appearance_mode)))
|
||||
self.text_label.configure(fg=(ThemeManager.single_color(self.text_color_disabled, self._appearance_mode)))
|
||||
else:
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self.appearance_mode))
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
|
||||
if self.fg_color is None:
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
else:
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
self.text_label.configure(text=self.text) # set text
|
||||
|
||||
@ -176,9 +183,9 @@ class CTkButton(CTkBaseClass):
|
||||
if no_color_updates is False:
|
||||
# set image_label bg color (background color of label)
|
||||
if self.fg_color is None:
|
||||
self.image_label.configure(bg=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
self.image_label.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
else:
|
||||
self.image_label.configure(bg=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
self.image_label.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
self.image_label.configure(image=self.image) # set image
|
||||
|
||||
@ -290,6 +297,14 @@ class CTkButton(CTkBaseClass):
|
||||
self.text_label.configure(textvariable=self.textvariable)
|
||||
del kwargs["textvariable"]
|
||||
|
||||
if "width" in kwargs:
|
||||
self.set_dimensions(width=kwargs["width"])
|
||||
del kwargs["width"]
|
||||
|
||||
if "height" in kwargs:
|
||||
self.set_dimensions(height=kwargs["height"])
|
||||
del kwargs["height"]
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
if require_redraw:
|
||||
@ -326,16 +341,16 @@ class CTkButton(CTkBaseClass):
|
||||
|
||||
# set color of inner button parts to hover color
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
outline=ThemeManager.single_color(inner_parts_color, self.appearance_mode),
|
||||
fill=ThemeManager.single_color(inner_parts_color, self.appearance_mode))
|
||||
outline=ThemeManager.single_color(inner_parts_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(inner_parts_color, self._appearance_mode))
|
||||
|
||||
# set text_label bg color to button hover color
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(bg=ThemeManager.single_color(inner_parts_color, self.appearance_mode))
|
||||
self.text_label.configure(bg=ThemeManager.single_color(inner_parts_color, self._appearance_mode))
|
||||
|
||||
# set image_label bg color to button hover color
|
||||
if self.image_label is not None:
|
||||
self.image_label.configure(bg=ThemeManager.single_color(inner_parts_color, self.appearance_mode))
|
||||
self.image_label.configure(bg=ThemeManager.single_color(inner_parts_color, self._appearance_mode))
|
||||
|
||||
def on_leave(self, event=0):
|
||||
self.click_animation_running = False
|
||||
@ -348,16 +363,16 @@ class CTkButton(CTkBaseClass):
|
||||
|
||||
# set color of inner button parts
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
outline=ThemeManager.single_color(inner_parts_color, self.appearance_mode),
|
||||
fill=ThemeManager.single_color(inner_parts_color, self.appearance_mode))
|
||||
outline=ThemeManager.single_color(inner_parts_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(inner_parts_color, self._appearance_mode))
|
||||
|
||||
# set text_label bg color (label color)
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(bg=ThemeManager.single_color(inner_parts_color, self.appearance_mode))
|
||||
self.text_label.configure(bg=ThemeManager.single_color(inner_parts_color, self._appearance_mode))
|
||||
|
||||
# set image_label bg color (image bg color)
|
||||
if self.image_label is not None:
|
||||
self.image_label.configure(bg=ThemeManager.single_color(inner_parts_color, self.appearance_mode))
|
||||
self.image_label.configure(bg=ThemeManager.single_color(inner_parts_color, self._appearance_mode))
|
||||
|
||||
def click_animation(self):
|
||||
if self.click_animation_running:
|
||||
|
@ -1,5 +1,6 @@
|
||||
import tkinter
|
||||
import sys
|
||||
from typing import Union
|
||||
|
||||
from .ctk_canvas import CTkCanvas
|
||||
from ..theme_manager import ThemeManager
|
||||
@ -34,7 +35,7 @@ class CTkCheckBox(CTkBaseClass):
|
||||
textvariable=None,
|
||||
**kwargs):
|
||||
|
||||
# transfer basic functionality (bg_color, size, appearance_mode, scaling) to CTkBaseClass
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
|
||||
|
||||
# color
|
||||
@ -49,7 +50,7 @@ class CTkCheckBox(CTkBaseClass):
|
||||
|
||||
# text
|
||||
self.text = text
|
||||
self.text_label = None
|
||||
self.text_label: Union[tkinter.Label, None] = None
|
||||
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
||||
self.text_color_disabled = ThemeManager.theme["color"]["text_disabled"] if text_color_disabled == "default_theme" else text_color_disabled
|
||||
self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
|
||||
@ -74,22 +75,19 @@ class CTkCheckBox(CTkBaseClass):
|
||||
|
||||
self.bg_canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self.desired_width),
|
||||
height=self.apply_widget_scaling(self.desired_height))
|
||||
width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.bg_canvas.grid(row=0, column=0, padx=0, pady=0, columnspan=3, rowspan=1, sticky="nswe")
|
||||
|
||||
self.canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self.desired_width),
|
||||
height=self.apply_widget_scaling(self.desired_height))
|
||||
width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.canvas.grid(row=0, column=0, padx=0, pady=0, columnspan=1, rowspan=1)
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
if self.hover is True:
|
||||
self.canvas.bind("<Enter>", self.on_enter)
|
||||
self.canvas.bind("<Leave>", self.on_leave)
|
||||
|
||||
self.canvas.bind("<Button-1>", self.toggle)
|
||||
self.canvas.bind("<Enter>", self.on_enter)
|
||||
self.canvas.bind("<Leave>", self.on_leave)
|
||||
self.canvas.bind("<Button-1>", self.toggle)
|
||||
|
||||
# set select state according to variable
|
||||
@ -100,8 +98,8 @@ class CTkCheckBox(CTkBaseClass):
|
||||
elif self.variable.get() == self.offvalue:
|
||||
self.deselect(from_variable_callback=True)
|
||||
|
||||
self.set_cursor()
|
||||
self.draw() # initial draw
|
||||
self.set_cursor()
|
||||
|
||||
def set_scaling(self, *args, **kwargs):
|
||||
super().set_scaling(*args, **kwargs)
|
||||
@ -109,8 +107,8 @@ class CTkCheckBox(CTkBaseClass):
|
||||
self.grid_columnconfigure(1, weight=0, minsize=self.apply_widget_scaling(6))
|
||||
self.text_label.configure(font=self.apply_font_scaling(self.text_font))
|
||||
|
||||
self.bg_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.bg_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()
|
||||
|
||||
def destroy(self):
|
||||
@ -120,40 +118,40 @@ class CTkCheckBox(CTkBaseClass):
|
||||
super().destroy()
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self.current_width),
|
||||
self.apply_widget_scaling(self.current_height),
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
self.apply_widget_scaling(self.border_width))
|
||||
|
||||
if self.check_state is True:
|
||||
self.draw_engine.draw_checkmark(self.apply_widget_scaling(self.current_width),
|
||||
self.apply_widget_scaling(self.current_height),
|
||||
self.apply_widget_scaling(self.current_height * 0.58))
|
||||
self.draw_engine.draw_checkmark(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self._current_height * 0.58))
|
||||
else:
|
||||
self.canvas.delete("checkmark")
|
||||
|
||||
self.bg_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.bg_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))
|
||||
|
||||
if self.check_state is True:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
fill=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("border_parts",
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
fill=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
if "create_line" in self.canvas.gettags("checkmark"):
|
||||
self.canvas.itemconfig("checkmark", fill=ThemeManager.single_color(self.checkmark_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("checkmark", fill=ThemeManager.single_color(self.checkmark_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("checkmark", fill=ThemeManager.single_color(self.checkmark_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("checkmark", fill=ThemeManager.single_color(self.checkmark_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
outline=ThemeManager.single_color(self.bg_color, self.appearance_mode),
|
||||
fill=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("border_parts",
|
||||
outline=ThemeManager.single_color(self.border_color, self.appearance_mode),
|
||||
fill=ThemeManager.single_color(self.border_color, self.appearance_mode))
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
|
||||
if self.text_label is None:
|
||||
self.text_label = tkinter.Label(master=self,
|
||||
@ -164,11 +162,15 @@ class CTkCheckBox(CTkBaseClass):
|
||||
self.text_label.grid(row=0, column=2, padx=0, pady=0, sticky="w")
|
||||
self.text_label["anchor"] = "w"
|
||||
|
||||
self.text_label.bind("<Enter>", self.on_enter)
|
||||
self.text_label.bind("<Leave>", self.on_leave)
|
||||
self.text_label.bind("<Button-1>", self.toggle)
|
||||
|
||||
if self.state == tkinter.DISABLED:
|
||||
self.text_label.configure(fg=(ThemeManager.single_color(self.text_color_disabled, self.appearance_mode)))
|
||||
self.text_label.configure(fg=(ThemeManager.single_color(self.text_color_disabled, self._appearance_mode)))
|
||||
else:
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self.appearance_mode))
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
|
||||
self.set_text(self.text)
|
||||
|
||||
@ -244,14 +246,22 @@ class CTkCheckBox(CTkBaseClass):
|
||||
if self.state == tkinter.DISABLED:
|
||||
if sys.platform == "darwin" and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="arrow")
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="arrow")
|
||||
elif sys.platform.startswith("win") and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="arrow")
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="arrow")
|
||||
|
||||
elif self.state == tkinter.NORMAL:
|
||||
if sys.platform == "darwin" and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="pointinghand")
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="pointinghand")
|
||||
elif sys.platform.startswith("win") and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="hand2")
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="hand2")
|
||||
|
||||
def set_text(self, text):
|
||||
self.text = text
|
||||
@ -264,32 +274,32 @@ class CTkCheckBox(CTkBaseClass):
|
||||
if self.hover is True and self.state == tkinter.NORMAL:
|
||||
if self.check_state is True:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
fill=ThemeManager.single_color(self.hover_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.hover_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.hover_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.hover_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("border_parts",
|
||||
fill=ThemeManager.single_color(self.hover_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.hover_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.hover_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.hover_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
fill=ThemeManager.single_color(self.hover_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.hover_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.hover_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.hover_color, self._appearance_mode))
|
||||
|
||||
def on_leave(self, event=0):
|
||||
if self.hover is True:
|
||||
if self.check_state is True:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
fill=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("border_parts",
|
||||
fill=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
fill=ThemeManager.single_color(self.bg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("border_parts",
|
||||
fill=ThemeManager.single_color(self.border_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.border_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
|
||||
def variable_callback(self, var_name, index, mode):
|
||||
if not self.variable_callback_blocked:
|
||||
@ -325,10 +335,7 @@ class CTkCheckBox(CTkBaseClass):
|
||||
self.variable_callback_blocked = False
|
||||
|
||||
if self.function is not None:
|
||||
try:
|
||||
self.function()
|
||||
except:
|
||||
pass
|
||||
self.function()
|
||||
|
||||
def deselect(self, from_variable_callback=False):
|
||||
self.check_state = False
|
||||
|
305
customtkinter/widgets/ctk_combobox.py
Normal file
305
customtkinter/widgets/ctk_combobox.py
Normal file
@ -0,0 +1,305 @@
|
||||
import tkinter
|
||||
import sys
|
||||
from typing import Union
|
||||
|
||||
from .dropdown_menu import DropdownMenu
|
||||
|
||||
from .ctk_canvas import CTkCanvas
|
||||
from ..theme_manager import ThemeManager
|
||||
from ..settings import Settings
|
||||
from ..draw_engine import DrawEngine
|
||||
from .widget_base_class import CTkBaseClass
|
||||
|
||||
|
||||
class CTkComboBox(CTkBaseClass):
|
||||
|
||||
def __init__(self, *args,
|
||||
bg_color=None,
|
||||
fg_color="default_theme",
|
||||
border_color="default_theme",
|
||||
button_color="default_theme",
|
||||
button_hover_color="default_theme",
|
||||
dropdown_color="default_theme",
|
||||
dropdown_hover_color="default_theme",
|
||||
dropdown_text_color="default_theme",
|
||||
variable=None,
|
||||
values=None,
|
||||
command=None,
|
||||
width=140,
|
||||
height=28,
|
||||
corner_radius="default_theme",
|
||||
border_width="default_theme",
|
||||
text_font="default_theme",
|
||||
text_color="default_theme",
|
||||
text_color_disabled="default_theme",
|
||||
hover=True,
|
||||
state=tkinter.NORMAL,
|
||||
**kwargs):
|
||||
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
|
||||
|
||||
# color variables
|
||||
self.fg_color = ThemeManager.theme["color"]["entry"] if fg_color == "default_theme" else fg_color
|
||||
self.border_color = ThemeManager.theme["color"]["combobox_border"] if border_color == "default_theme" else border_color
|
||||
self.button_color = ThemeManager.theme["color"]["combobox_border"] if button_color == "default_theme" else button_color
|
||||
self.button_hover_color = ThemeManager.theme["color"]["combobox_button_hover"] if button_hover_color == "default_theme" else button_hover_color
|
||||
self.dropdown_color = ThemeManager.theme["color"]["dropdown_color"] if dropdown_color == "default_theme" else dropdown_color
|
||||
self.dropdown_hover_color = ThemeManager.theme["color"]["dropdown_hover"] if dropdown_hover_color == "default_theme" else dropdown_hover_color
|
||||
self.dropdown_text_color = ThemeManager.theme["color"]["dropdown_text"] if dropdown_text_color == "default_theme" else dropdown_text_color
|
||||
|
||||
# shape
|
||||
self.corner_radius = ThemeManager.theme["shape"]["button_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
||||
self.border_width = ThemeManager.theme["shape"]["entry_border_width"] if border_width == "default_theme" else border_width
|
||||
|
||||
# text and font
|
||||
self.text_label = None
|
||||
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
||||
self.text_color_disabled = ThemeManager.theme["color"]["text_button_disabled"] if text_color_disabled == "default_theme" else text_color_disabled
|
||||
self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
|
||||
|
||||
# callback and hover functionality
|
||||
self.function = command
|
||||
self.variable = variable
|
||||
self.state = state
|
||||
self.hover = hover
|
||||
self.click_animation_running = False
|
||||
|
||||
if values is None:
|
||||
self.values = ["CTkComboBox"]
|
||||
else:
|
||||
self.values = values
|
||||
|
||||
if len(self.values) > 0:
|
||||
self.current_value = self.values[0]
|
||||
else:
|
||||
self.current_value = "CTkComboBox"
|
||||
|
||||
self.dropdown_menu: Union[DropdownMenu, None] = None
|
||||
|
||||
# configure grid system (1x1)
|
||||
self.grid_rowconfigure(0, weight=1)
|
||||
self.grid_columnconfigure(0, weight=1)
|
||||
|
||||
self.canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.canvas.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="nsew")
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
self.entry = tkinter.Entry(master=self,
|
||||
state=self.state,
|
||||
width=1,
|
||||
bd=0,
|
||||
highlightthickness=0,
|
||||
font=self.apply_font_scaling(self.text_font))
|
||||
left_section_width = self._current_width - self._current_height
|
||||
self.entry.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="ew",
|
||||
padx=(self.apply_widget_scaling(max(self.corner_radius, 3)),
|
||||
self.apply_widget_scaling(max(self._current_width - left_section_width + 3, 3))))
|
||||
|
||||
self.draw() # initial draw
|
||||
|
||||
# event bindings
|
||||
self.canvas.tag_bind("right_parts", "<Enter>", self.on_enter)
|
||||
self.canvas.tag_bind("dropdown_arrow", "<Enter>", self.on_enter)
|
||||
self.canvas.tag_bind("right_parts", "<Leave>", self.on_leave)
|
||||
self.canvas.tag_bind("dropdown_arrow", "<Leave>", self.on_leave)
|
||||
self.canvas.tag_bind("right_parts", "<Button-1>", self.clicked)
|
||||
self.canvas.tag_bind("dropdown_arrow", "<Button-1>", self.clicked)
|
||||
self.bind('<Configure>', self.update_dimensions_event)
|
||||
|
||||
if self.variable is not None:
|
||||
self.entry.configure(textvariable=self.variable)
|
||||
|
||||
def set_scaling(self, *args, **kwargs):
|
||||
super().set_scaling(*args, **kwargs)
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
|
||||
def set_dimensions(self, width: int = None, height: int = None):
|
||||
super().set_dimensions(width, height)
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
left_section_width = self._current_width - self._current_height
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border_vertical_split(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
self.apply_widget_scaling(self.border_width),
|
||||
self.apply_widget_scaling(left_section_width))
|
||||
|
||||
requires_recoloring_2 = self.draw_engine.draw_dropdown_arrow(self.apply_widget_scaling(self._current_width - (self._current_height / 2)),
|
||||
self.apply_widget_scaling(self._current_height / 2),
|
||||
self.apply_widget_scaling(self._current_height / 3))
|
||||
|
||||
if self.current_value is not None:
|
||||
self.entry.delete(0, tkinter.END)
|
||||
self.entry.insert(0, self.current_value)
|
||||
|
||||
if no_color_updates is False or requires_recoloring or requires_recoloring_2:
|
||||
|
||||
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
|
||||
self.canvas.itemconfig("inner_parts_left",
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("border_parts_left",
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("inner_parts_right",
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("border_parts_right",
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
|
||||
self.entry.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
self.entry.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
if self.state == tkinter.DISABLED:
|
||||
self.entry.configure(fg=(ThemeManager.single_color(self.text_color_disabled, self._appearance_mode)))
|
||||
self.canvas.itemconfig("dropdown_arrow",
|
||||
fill=ThemeManager.single_color(self.text_color_disabled, self._appearance_mode))
|
||||
else:
|
||||
self.entry.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("dropdown_arrow",
|
||||
fill=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
|
||||
def open_dropdown_menu(self):
|
||||
self.dropdown_menu = DropdownMenu(x_position=self.winfo_rootx(),
|
||||
y_position=self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 4),
|
||||
width=self._current_width,
|
||||
values=self.values,
|
||||
command=self.set,
|
||||
fg_color=self.dropdown_color,
|
||||
button_hover_color=self.dropdown_hover_color,
|
||||
button_color=self.dropdown_color,
|
||||
text_color=self.dropdown_text_color)
|
||||
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
|
||||
if "state" in kwargs:
|
||||
self.state = kwargs["state"]
|
||||
self.entry.configure(state=self.state)
|
||||
require_redraw = True
|
||||
del kwargs["state"]
|
||||
|
||||
if "fg_color" in kwargs:
|
||||
self.fg_color = kwargs["fg_color"]
|
||||
require_redraw = True
|
||||
del kwargs["fg_color"]
|
||||
|
||||
if "bg_color" in kwargs:
|
||||
if kwargs["bg_color"] is None:
|
||||
self.bg_color = self.detect_color_of_master()
|
||||
else:
|
||||
self.bg_color = kwargs["bg_color"]
|
||||
require_redraw = True
|
||||
del kwargs["bg_color"]
|
||||
|
||||
if "button_color" in kwargs:
|
||||
self.button_color = kwargs["button_color"]
|
||||
require_redraw = True
|
||||
del kwargs["button_color"]
|
||||
|
||||
if "button_hover_color" in kwargs:
|
||||
self.button_hover_color = kwargs["button_hover_color"]
|
||||
require_redraw = True
|
||||
del kwargs["button_hover_color"]
|
||||
|
||||
if "text_color" in kwargs:
|
||||
self.text_color = kwargs["text_color"]
|
||||
require_redraw = True
|
||||
del kwargs["text_color"]
|
||||
|
||||
if "command" in kwargs:
|
||||
self.function = kwargs["command"]
|
||||
del kwargs["command"]
|
||||
|
||||
if "variable" in kwargs:
|
||||
self.variable = kwargs["variable"]
|
||||
self.entry.configure(textvariable=self.variable)
|
||||
del kwargs["variable"]
|
||||
|
||||
if "width" in kwargs:
|
||||
self.set_dimensions(width=kwargs["width"])
|
||||
del kwargs["width"]
|
||||
|
||||
if "height" in kwargs:
|
||||
self.set_dimensions(height=kwargs["height"])
|
||||
del kwargs["height"]
|
||||
|
||||
if "values" in kwargs:
|
||||
self.values = kwargs["values"]
|
||||
del kwargs["values"]
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
if require_redraw:
|
||||
self.draw()
|
||||
|
||||
def on_enter(self, event=0):
|
||||
if self.hover is True and self.state == tkinter.NORMAL and len(self.values) > 0:
|
||||
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="pointinghand")
|
||||
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="hand2")
|
||||
|
||||
# set color of inner button parts to hover color
|
||||
self.canvas.itemconfig("inner_parts_right",
|
||||
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("border_parts_right",
|
||||
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
|
||||
|
||||
def on_leave(self, event=0):
|
||||
self.click_animation_running = False
|
||||
|
||||
if self.hover is True:
|
||||
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="arrow")
|
||||
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="arrow")
|
||||
|
||||
# set color of inner button parts
|
||||
self.canvas.itemconfig("inner_parts_right",
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("border_parts_right",
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
|
||||
def click_animation(self):
|
||||
if self.click_animation_running:
|
||||
self.on_enter()
|
||||
|
||||
def set(self, value: str, from_variable_callback: bool = False):
|
||||
self.current_value = value
|
||||
|
||||
self.entry.delete(0, tkinter.END)
|
||||
self.entry.insert(0, self.current_value)
|
||||
|
||||
if not from_variable_callback:
|
||||
if self.function is not None:
|
||||
self.function(self.current_value)
|
||||
|
||||
def get(self) -> str:
|
||||
return self.entry.get()
|
||||
|
||||
def clicked(self, event=0):
|
||||
if self.state is not tkinter.DISABLED and len(self.values) > 0:
|
||||
self.open_dropdown_menu()
|
||||
|
||||
# click animation: change color with .on_leave() and back to normal after 100ms with click_animation()
|
||||
self.on_leave()
|
||||
self.click_animation_running = True
|
||||
self.after(100, self.click_animation)
|
@ -2,7 +2,6 @@ import tkinter
|
||||
|
||||
from .ctk_canvas import CTkCanvas
|
||||
from ..theme_manager import ThemeManager
|
||||
from ..settings import Settings
|
||||
from ..draw_engine import DrawEngine
|
||||
from .widget_base_class import CTkBaseClass
|
||||
|
||||
@ -18,11 +17,12 @@ class CTkEntry(CTkBaseClass):
|
||||
corner_radius="default_theme",
|
||||
border_width="default_theme",
|
||||
border_color="default_theme",
|
||||
width=120,
|
||||
height=30,
|
||||
width=140,
|
||||
height=28,
|
||||
state=tkinter.NORMAL,
|
||||
**kwargs):
|
||||
|
||||
# transfer basic functionality (bg_color, size, appearance_mode, scaling) to CTkBaseClass
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
if "master" in kwargs:
|
||||
super().__init__(*args, bg_color=bg_color, width=width, height=height, master=kwargs["master"])
|
||||
del kwargs["master"]
|
||||
@ -42,13 +42,15 @@ class CTkEntry(CTkBaseClass):
|
||||
self.placeholder_text_active = False
|
||||
self.pre_placeholder_arguments = {} # some set arguments of the entry will be changed for placeholder and then set back
|
||||
|
||||
self.state = state
|
||||
|
||||
self.corner_radius = ThemeManager.theme["shape"]["button_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
||||
self.border_width = ThemeManager.theme["shape"]["entry_border_width"] if border_width == "default_theme" else border_width
|
||||
|
||||
self.canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self.current_width),
|
||||
height=self.apply_widget_scaling(self.current_height))
|
||||
width=self.apply_widget_scaling(self._current_width),
|
||||
height=self.apply_widget_scaling(self._current_height))
|
||||
self.canvas.grid(column=0, row=0, sticky="we")
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
@ -57,6 +59,7 @@ class CTkEntry(CTkBaseClass):
|
||||
width=1,
|
||||
highlightthickness=0,
|
||||
font=self.apply_font_scaling(self.text_font),
|
||||
state=self.state,
|
||||
**kwargs)
|
||||
self.entry.grid(column=0, row=0, sticky="we",
|
||||
padx=self.apply_widget_scaling(self.corner_radius) if self.corner_radius >= 6 else self.apply_widget_scaling(6))
|
||||
@ -75,7 +78,14 @@ class CTkEntry(CTkBaseClass):
|
||||
self.entry.grid(column=0, row=0, sticky="we",
|
||||
padx=self.apply_widget_scaling(self.corner_radius) if self.corner_radius >= 6 else self.apply_widget_scaling(6))
|
||||
|
||||
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()
|
||||
|
||||
def set_dimensions(self, width=None, height=None):
|
||||
super().set_dimensions(width, height)
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
|
||||
def set_placeholder(self, event=None):
|
||||
@ -83,54 +93,54 @@ class CTkEntry(CTkBaseClass):
|
||||
if not self.placeholder_text_active and self.entry.get() == "":
|
||||
self.placeholder_text_active = True
|
||||
self.pre_placeholder_arguments = {"show": self.entry.cget("show")}
|
||||
self.entry.config(fg=ThemeManager.single_color(self.placeholder_text_color, self.appearance_mode), show="")
|
||||
self.entry.config(fg=ThemeManager.single_color(self.placeholder_text_color, self._appearance_mode), show="")
|
||||
self.entry.delete(0, tkinter.END)
|
||||
self.entry.insert(0, self.placeholder_text)
|
||||
|
||||
def clear_placeholder(self, event=None):
|
||||
if self.placeholder_text_active:
|
||||
self.placeholder_text_active = False
|
||||
self.entry.config(fg=ThemeManager.single_color(self.text_color, self.appearance_mode))
|
||||
self.entry.config(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
self.entry.delete(0, tkinter.END)
|
||||
for argument, value in self.pre_placeholder_arguments.items():
|
||||
self.entry[argument] = value
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
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))
|
||||
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self.current_width),
|
||||
self.apply_widget_scaling(self.current_height),
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
self.apply_widget_scaling(self.border_width))
|
||||
|
||||
if requires_recoloring or no_color_updates is False:
|
||||
if ThemeManager.single_color(self.fg_color, self.appearance_mode) is not None:
|
||||
if ThemeManager.single_color(self.fg_color, self._appearance_mode) is not None:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
fill=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
self.entry.configure(bg=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
disabledbackground=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
highlightcolor=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
fg=ThemeManager.single_color(self.text_color, self.appearance_mode),
|
||||
disabledforeground=ThemeManager.single_color(self.text_color, self.appearance_mode),
|
||||
insertbackground=ThemeManager.single_color(self.text_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
self.entry.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
disabledbackground=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
highlightcolor=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
fg=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
||||
disabledforeground=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
||||
insertbackground=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
fill=ThemeManager.single_color(self.bg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
self.entry.configure(bg=ThemeManager.single_color(self.bg_color, self.appearance_mode),
|
||||
disabledbackground=ThemeManager.single_color(self.bg_color, self.appearance_mode),
|
||||
highlightcolor=ThemeManager.single_color(self.bg_color, self.appearance_mode),
|
||||
fg=ThemeManager.single_color(self.text_color, self.appearance_mode),
|
||||
disabledforeground=ThemeManager.single_color(self.text_color, self.appearance_mode),
|
||||
insertbackground=ThemeManager.single_color(self.text_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
self.entry.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
disabledbackground=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
highlightcolor=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
fg=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
||||
disabledforeground=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
||||
insertbackground=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
|
||||
self.canvas.itemconfig("border_parts",
|
||||
fill=ThemeManager.single_color(self.border_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.border_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
|
||||
if self.placeholder_text_active:
|
||||
self.entry.config(fg=ThemeManager.single_color(self.placeholder_text_color, self.appearance_mode))
|
||||
self.entry.config(fg=ThemeManager.single_color(self.placeholder_text_color, self._appearance_mode))
|
||||
|
||||
def bind(self, *args, **kwargs):
|
||||
self.entry.bind(*args, **kwargs)
|
||||
@ -138,6 +148,11 @@ class CTkEntry(CTkBaseClass):
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
|
||||
if "state" in kwargs:
|
||||
self.state = kwargs["state"]
|
||||
self.entry.configure(state=self.state)
|
||||
del kwargs["state"]
|
||||
|
||||
if "bg_color" in kwargs:
|
||||
if kwargs["bg_color"] is None:
|
||||
self.bg_color = self.detect_color_of_master()
|
||||
@ -164,15 +179,23 @@ class CTkEntry(CTkBaseClass):
|
||||
if "corner_radius" in kwargs:
|
||||
self.corner_radius = kwargs["corner_radius"]
|
||||
|
||||
if self.corner_radius * 2 > self.current_height:
|
||||
self.corner_radius = self.current_height / 2
|
||||
elif self.corner_radius * 2 > self.current_width:
|
||||
self.corner_radius = self.current_width / 2
|
||||
if self.corner_radius * 2 > self._current_height:
|
||||
self.corner_radius = self._current_height / 2
|
||||
elif self.corner_radius * 2 > self._current_width:
|
||||
self.corner_radius = self._current_width / 2
|
||||
|
||||
self.entry.grid(column=0, row=0, sticky="we", padx=self.apply_widget_scaling(self.corner_radius) if self.corner_radius >= 6 else self.apply_widget_scaling(6))
|
||||
del kwargs["corner_radius"]
|
||||
require_redraw = True
|
||||
|
||||
if "width" in kwargs:
|
||||
self.set_dimensions(width=kwargs["width"])
|
||||
del kwargs["width"]
|
||||
|
||||
if "height" in kwargs:
|
||||
self.set_dimensions(height=kwargs["height"])
|
||||
del kwargs["height"]
|
||||
|
||||
if "placeholder_text" in kwargs:
|
||||
pass
|
||||
|
||||
|
@ -1,8 +1,5 @@
|
||||
import tkinter
|
||||
|
||||
from .ctk_canvas import CTkCanvas
|
||||
from ..theme_manager import ThemeManager
|
||||
from ..settings import Settings
|
||||
from ..draw_engine import DrawEngine
|
||||
from .widget_base_class import CTkBaseClass
|
||||
|
||||
@ -16,14 +13,16 @@ class CTkFrame(CTkBaseClass):
|
||||
corner_radius="default_theme",
|
||||
width=200,
|
||||
height=200,
|
||||
overwrite_preferred_drawing_method: str = None,
|
||||
**kwargs):
|
||||
|
||||
# transfer basic functionality (bg_color, size, appearance_mode, scaling) to CTkBaseClass
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
|
||||
|
||||
# color
|
||||
self.border_color = ThemeManager.theme["color"]["frame_border"] if border_color == "default_theme" else border_color
|
||||
|
||||
# determine fg_color of frame
|
||||
if fg_color == "default_theme":
|
||||
if isinstance(self.master, CTkFrame):
|
||||
if self.master.fg_color == ThemeManager.theme["color"]["frame_low"]:
|
||||
@ -41,11 +40,12 @@ class CTkFrame(CTkBaseClass):
|
||||
|
||||
self.canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self.current_width),
|
||||
height=self.apply_widget_scaling(self.current_height))
|
||||
width=self.apply_widget_scaling(self._current_width),
|
||||
height=self.apply_widget_scaling(self._current_height))
|
||||
self.canvas.place(x=0, y=0, relwidth=1, relheight=1)
|
||||
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.draw_engine = DrawEngine(self.canvas)
|
||||
self._overwrite_preferred_drawing_method = overwrite_preferred_drawing_method
|
||||
|
||||
self.bind('<Configure>', self.update_dimensions_event)
|
||||
|
||||
@ -65,30 +65,38 @@ class CTkFrame(CTkBaseClass):
|
||||
def set_scaling(self, *args, **kwargs):
|
||||
super().set_scaling(*args, **kwargs)
|
||||
|
||||
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()
|
||||
|
||||
def set_dimensions(self, width=None, height=None):
|
||||
super().set_dimensions(width, height)
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self.current_width),
|
||||
self.apply_widget_scaling(self.current_height),
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
self.apply_widget_scaling(self.border_width))
|
||||
self.apply_widget_scaling(self.border_width),
|
||||
overwrite_preferred_drawing_method=self._overwrite_preferred_drawing_method)
|
||||
|
||||
if no_color_updates is False or requires_recoloring:
|
||||
if self.fg_color is None:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
fill=ThemeManager.single_color(self.bg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
fill=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
self.canvas.itemconfig("border_parts",
|
||||
fill=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))
|
||||
fill=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.tag_lower("inner_parts")
|
||||
self.canvas.tag_lower("border_parts")
|
||||
@ -115,11 +123,29 @@ class CTkFrame(CTkBaseClass):
|
||||
|
||||
del kwargs["bg_color"]
|
||||
|
||||
if "border_color" in kwargs:
|
||||
self.border_color = kwargs["border_color"]
|
||||
require_redraw = True
|
||||
del kwargs["border_color"]
|
||||
|
||||
if "corner_radius" in kwargs:
|
||||
self.corner_radius = kwargs["corner_radius"]
|
||||
require_redraw = True
|
||||
del kwargs["corner_radius"]
|
||||
|
||||
if "border_width" in kwargs:
|
||||
self.border_width = kwargs["border_width"]
|
||||
require_redraw = True
|
||||
del kwargs["border_width"]
|
||||
|
||||
if "width" in kwargs:
|
||||
self.set_dimensions(width=kwargs["width"])
|
||||
del kwargs["width"]
|
||||
|
||||
if "height" in kwargs:
|
||||
self.set_dimensions(height=kwargs["height"])
|
||||
del kwargs["height"]
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
if require_redraw:
|
||||
|
@ -2,7 +2,6 @@ import tkinter
|
||||
|
||||
from .ctk_canvas import CTkCanvas
|
||||
from ..theme_manager import ThemeManager
|
||||
from ..settings import Settings
|
||||
from ..draw_engine import DrawEngine
|
||||
from .widget_base_class import CTkBaseClass
|
||||
|
||||
@ -13,13 +12,13 @@ class CTkLabel(CTkBaseClass):
|
||||
fg_color="default_theme",
|
||||
text_color="default_theme",
|
||||
corner_radius="default_theme",
|
||||
width=120,
|
||||
height=25,
|
||||
width=140,
|
||||
height=28,
|
||||
text="CTkLabel",
|
||||
text_font="default_theme",
|
||||
**kwargs):
|
||||
|
||||
# transfer basic functionality (bg_color, size, appearance_mode, scaling) to CTkBaseClass
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
if "master" in kwargs:
|
||||
super().__init__(*args, bg_color=bg_color, width=width, height=height, master=kwargs["master"])
|
||||
del kwargs["master"]
|
||||
@ -45,8 +44,8 @@ class CTkLabel(CTkBaseClass):
|
||||
|
||||
self.canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self.desired_width),
|
||||
height=self.apply_widget_scaling(self.desired_height))
|
||||
width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.canvas.grid(row=0, column=0, sticky="nswe")
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
@ -64,33 +63,42 @@ class CTkLabel(CTkBaseClass):
|
||||
def set_scaling(self, *args, **kwargs):
|
||||
super().set_scaling(*args, **kwargs)
|
||||
|
||||
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.text_label.configure(font=self.apply_font_scaling(self.text_font))
|
||||
self.text_label.grid(row=0, column=0, padx=self.apply_widget_scaling(self.corner_radius))
|
||||
|
||||
self.draw()
|
||||
|
||||
def set_dimensions(self, width=None, height=None):
|
||||
super().set_dimensions(width, height)
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self.current_width),
|
||||
self.apply_widget_scaling(self.current_height),
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
0)
|
||||
|
||||
if no_color_updates is False or requires_recoloring:
|
||||
if ThemeManager.single_color(self.fg_color, self.appearance_mode) is not None:
|
||||
if ThemeManager.single_color(self.fg_color, self._appearance_mode) is not None:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
fill=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self.appearance_mode),
|
||||
bg=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
||||
bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
fill=ThemeManager.single_color(self.bg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self.appearance_mode),
|
||||
bg=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
||||
bg=ThemeManager.single_color(self.bg_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))
|
||||
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
@ -117,6 +125,14 @@ class CTkLabel(CTkBaseClass):
|
||||
require_redraw = True
|
||||
del kwargs["text_color"]
|
||||
|
||||
if "width" in kwargs:
|
||||
self.set_dimensions(width=kwargs["width"])
|
||||
del kwargs["width"]
|
||||
|
||||
if "height" in kwargs:
|
||||
self.set_dimensions(height=kwargs["height"])
|
||||
del kwargs["height"]
|
||||
|
||||
self.text_label.configure(*args, **kwargs)
|
||||
|
||||
if require_redraw:
|
||||
|
313
customtkinter/widgets/ctk_optionmenu.py
Normal file
313
customtkinter/widgets/ctk_optionmenu.py
Normal file
@ -0,0 +1,313 @@
|
||||
import tkinter
|
||||
import sys
|
||||
from typing import Union
|
||||
|
||||
from .dropdown_menu import DropdownMenu
|
||||
|
||||
from .ctk_canvas import CTkCanvas
|
||||
from ..theme_manager import ThemeManager
|
||||
from ..settings import Settings
|
||||
from ..draw_engine import DrawEngine
|
||||
from .widget_base_class import CTkBaseClass
|
||||
|
||||
|
||||
class CTkOptionMenu(CTkBaseClass):
|
||||
|
||||
def __init__(self, *args,
|
||||
bg_color=None,
|
||||
fg_color="default_theme",
|
||||
button_color="default_theme",
|
||||
button_hover_color="default_theme",
|
||||
dropdown_color="default_theme",
|
||||
dropdown_hover_color="default_theme",
|
||||
dropdown_text_color="default_theme",
|
||||
variable=None,
|
||||
values=None,
|
||||
command=None,
|
||||
width=140,
|
||||
height=28,
|
||||
corner_radius="default_theme",
|
||||
text_font="default_theme",
|
||||
text_color="default_theme",
|
||||
text_color_disabled="default_theme",
|
||||
hover=True,
|
||||
state=tkinter.NORMAL,
|
||||
**kwargs):
|
||||
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
|
||||
|
||||
# color variables
|
||||
self.fg_color = ThemeManager.theme["color"]["button"] if fg_color == "default_theme" else fg_color
|
||||
self.button_color = ThemeManager.theme["color"]["optionmenu_button"] if button_color == "default_theme" else button_color
|
||||
self.button_hover_color = ThemeManager.theme["color"]["optionmenu_button_hover"] if button_hover_color == "default_theme" else button_hover_color
|
||||
self.dropdown_color = ThemeManager.theme["color"]["dropdown_color"] if dropdown_color == "default_theme" else dropdown_color
|
||||
self.dropdown_hover_color = ThemeManager.theme["color"]["dropdown_hover"] if dropdown_hover_color == "default_theme" else dropdown_hover_color
|
||||
self.dropdown_text_color = ThemeManager.theme["color"]["dropdown_text"] if dropdown_text_color == "default_theme" else dropdown_text_color
|
||||
|
||||
# shape
|
||||
self.corner_radius = ThemeManager.theme["shape"]["button_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
||||
|
||||
# text and font
|
||||
self.text_label = None
|
||||
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
||||
self.text_color_disabled = ThemeManager.theme["color"]["text_button_disabled"] if text_color_disabled == "default_theme" else text_color_disabled
|
||||
self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
|
||||
|
||||
# callback and hover functionality
|
||||
self.function = command
|
||||
self.variable = variable
|
||||
self.variable_callback_blocked = False
|
||||
self.variable_callback_name = None
|
||||
self.state = state
|
||||
self.hover = hover
|
||||
self.click_animation_running = False
|
||||
|
||||
if values is None:
|
||||
self.values = ["CTkOptionMenu"]
|
||||
else:
|
||||
self.values = values
|
||||
|
||||
if len(self.values) > 0:
|
||||
self.current_value = self.values[0]
|
||||
else:
|
||||
self.current_value = "CTkOptionMenu"
|
||||
|
||||
self.dropdown_menu: Union[DropdownMenu, None] = None
|
||||
|
||||
# configure grid system (1x1)
|
||||
self.grid_rowconfigure(0, weight=1)
|
||||
self.grid_columnconfigure(0, weight=1)
|
||||
|
||||
self.canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.canvas.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="nsew")
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
# event bindings
|
||||
self.canvas.bind("<Enter>", self.on_enter)
|
||||
self.canvas.bind("<Leave>", self.on_leave)
|
||||
self.canvas.bind("<Button-1>", self.clicked)
|
||||
self.canvas.bind("<Button-1>", self.clicked)
|
||||
self.bind('<Configure>', self.update_dimensions_event)
|
||||
|
||||
self.draw() # initial draw
|
||||
|
||||
if self.variable is not None:
|
||||
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
||||
self.set(self.variable.get(), from_variable_callback=True)
|
||||
|
||||
def set_scaling(self, *args, **kwargs):
|
||||
super().set_scaling(*args, **kwargs)
|
||||
|
||||
if self.text_label is not None:
|
||||
self.text_label.destroy()
|
||||
self.text_label = None
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
|
||||
def set_dimensions(self, width: int = None, height: int = None):
|
||||
super().set_dimensions(width, height)
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
left_section_width = self._current_width - self._current_height
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border_vertical_split(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
0,
|
||||
self.apply_widget_scaling(left_section_width))
|
||||
|
||||
requires_recoloring_2 = self.draw_engine.draw_dropdown_arrow(self.apply_widget_scaling(self._current_width - (self._current_height / 2)),
|
||||
self.apply_widget_scaling(self._current_height / 2),
|
||||
self.apply_widget_scaling(self._current_height / 3))
|
||||
if self.text_label is None:
|
||||
self.text_label = tkinter.Label(master=self,
|
||||
font=self.apply_font_scaling(self.text_font))
|
||||
self.text_label.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="w",
|
||||
padx=(max(self.apply_widget_scaling(self.corner_radius), 3),
|
||||
max(self._current_width - left_section_width + 3, 3)))
|
||||
|
||||
self.text_label.bind("<Enter>", self.on_enter)
|
||||
self.text_label.bind("<Leave>", self.on_leave)
|
||||
self.text_label.bind("<Button-1>", self.clicked)
|
||||
self.text_label.bind("<Button-1>", self.clicked)
|
||||
|
||||
if self.current_value is not None:
|
||||
self.text_label.configure(text=self.current_value)
|
||||
|
||||
if no_color_updates is False or requires_recoloring or requires_recoloring_2:
|
||||
|
||||
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
|
||||
self.canvas.itemconfig("inner_parts_left",
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("inner_parts_right",
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
|
||||
if self.state == tkinter.DISABLED:
|
||||
self.text_label.configure(fg=(ThemeManager.single_color(self.text_color_disabled, self._appearance_mode)))
|
||||
self.canvas.itemconfig("dropdown_arrow",
|
||||
fill=ThemeManager.single_color(self.text_color_disabled, self._appearance_mode))
|
||||
else:
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("dropdown_arrow",
|
||||
fill=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
def open_dropdown_menu(self):
|
||||
self.dropdown_menu = DropdownMenu(x_position=self.winfo_rootx(),
|
||||
y_position=self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 4),
|
||||
width=self._current_width,
|
||||
values=self.values,
|
||||
command=self.set,
|
||||
fg_color=self.dropdown_color,
|
||||
button_hover_color=self.dropdown_hover_color,
|
||||
button_color=self.dropdown_color,
|
||||
text_color=self.dropdown_text_color)
|
||||
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
|
||||
if "state" in kwargs:
|
||||
self.state = kwargs["state"]
|
||||
require_redraw = True
|
||||
del kwargs["state"]
|
||||
|
||||
if "fg_color" in kwargs:
|
||||
self.fg_color = kwargs["fg_color"]
|
||||
require_redraw = True
|
||||
del kwargs["fg_color"]
|
||||
|
||||
if "bg_color" in kwargs:
|
||||
if kwargs["bg_color"] is None:
|
||||
self.bg_color = self.detect_color_of_master()
|
||||
else:
|
||||
self.bg_color = kwargs["bg_color"]
|
||||
require_redraw = True
|
||||
del kwargs["bg_color"]
|
||||
|
||||
if "button_color" in kwargs:
|
||||
self.button_color = kwargs["button_color"]
|
||||
require_redraw = True
|
||||
del kwargs["button_color"]
|
||||
|
||||
if "button_hover_color" in kwargs:
|
||||
self.button_hover_color = kwargs["button_hover_color"]
|
||||
require_redraw = True
|
||||
del kwargs["button_hover_color"]
|
||||
|
||||
if "text_color" in kwargs:
|
||||
self.text_color = kwargs["text_color"]
|
||||
require_redraw = True
|
||||
del kwargs["text_color"]
|
||||
|
||||
if "command" in kwargs:
|
||||
self.function = kwargs["command"]
|
||||
del kwargs["command"]
|
||||
|
||||
if "variable" in kwargs:
|
||||
if self.variable is not None: # remove old callback
|
||||
self.variable.trace_remove("write", self.variable_callback_name)
|
||||
|
||||
self.variable = kwargs["variable"]
|
||||
|
||||
if self.variable is not None and self.variable != "":
|
||||
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
||||
self.set(self.variable.get(), from_variable_callback=True)
|
||||
else:
|
||||
self.variable = None
|
||||
|
||||
del kwargs["variable"]
|
||||
|
||||
if "width" in kwargs:
|
||||
self.set_dimensions(width=kwargs["width"])
|
||||
del kwargs["width"]
|
||||
|
||||
if "height" in kwargs:
|
||||
self.set_dimensions(height=kwargs["height"])
|
||||
del kwargs["height"]
|
||||
|
||||
if "values" in kwargs:
|
||||
self.values = kwargs["values"]
|
||||
del kwargs["values"]
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
if require_redraw:
|
||||
self.draw()
|
||||
|
||||
def on_enter(self, event=0):
|
||||
if self.hover is True and self.state == tkinter.NORMAL and len(self.values) > 0:
|
||||
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="pointinghand")
|
||||
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="hand2")
|
||||
|
||||
# set color of inner button parts to hover color
|
||||
self.canvas.itemconfig("inner_parts_right",
|
||||
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
|
||||
|
||||
def on_leave(self, event=0):
|
||||
self.click_animation_running = False
|
||||
|
||||
if self.hover is True:
|
||||
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="arrow")
|
||||
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="arrow")
|
||||
|
||||
# set color of inner button parts
|
||||
self.canvas.itemconfig("inner_parts_right",
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
|
||||
def click_animation(self):
|
||||
if self.click_animation_running:
|
||||
self.on_enter()
|
||||
|
||||
def variable_callback(self, var_name, index, mode):
|
||||
if not self.variable_callback_blocked:
|
||||
self.set(self.variable.get(), from_variable_callback=True)
|
||||
|
||||
def set(self, value: str, from_variable_callback: bool = False):
|
||||
self.current_value = value
|
||||
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(text=self.current_value)
|
||||
else:
|
||||
self.draw()
|
||||
|
||||
if self.variable is not None and not from_variable_callback:
|
||||
self.variable_callback_blocked = True
|
||||
self.variable.set(self.current_value)
|
||||
self.variable_callback_blocked = False
|
||||
|
||||
if not from_variable_callback:
|
||||
if self.function is not None:
|
||||
self.function(self.current_value)
|
||||
|
||||
def get(self) -> str:
|
||||
return self.current_value
|
||||
|
||||
def clicked(self, event=0):
|
||||
if self.state is not tkinter.DISABLED and len(self.values) > 0:
|
||||
self.open_dropdown_menu()
|
||||
|
||||
# click animation: change color with .on_leave() and back to normal after 100ms with click_animation()
|
||||
self.on_leave()
|
||||
self.click_animation_running = True
|
||||
self.after(100, self.click_animation)
|
@ -3,12 +3,11 @@ import tkinter
|
||||
from .ctk_canvas import CTkCanvas
|
||||
from ..theme_manager import ThemeManager
|
||||
from ..draw_engine import DrawEngine
|
||||
from ..settings import Settings
|
||||
from .widget_base_class import CTkBaseClass
|
||||
|
||||
|
||||
class CTkProgressBar(CTkBaseClass):
|
||||
""" tkinter custom progressbar, always horizontal, values are from 0 to 1 """
|
||||
""" tkinter custom progressbar, values from 0 to 1 """
|
||||
|
||||
def __init__(self, *args,
|
||||
variable=None,
|
||||
@ -35,7 +34,7 @@ class CTkProgressBar(CTkBaseClass):
|
||||
else:
|
||||
height = 8
|
||||
|
||||
# transfer basic functionality (bg_color, size, appearance_mode, scaling) to CTkBaseClass
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
|
||||
|
||||
# color
|
||||
@ -59,8 +58,8 @@ class CTkProgressBar(CTkBaseClass):
|
||||
|
||||
self.canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self.desired_width),
|
||||
height=self.apply_widget_scaling(self.desired_height))
|
||||
width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.canvas.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="nswe")
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
@ -78,7 +77,14 @@ class CTkProgressBar(CTkBaseClass):
|
||||
def set_scaling(self, *args, **kwargs):
|
||||
super().set_scaling(*args, **kwargs)
|
||||
|
||||
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()
|
||||
|
||||
def set_dimensions(self, width=None, height=None):
|
||||
super().set_dimensions(width, height)
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
|
||||
def destroy(self):
|
||||
@ -95,24 +101,24 @@ class CTkProgressBar(CTkBaseClass):
|
||||
else:
|
||||
orientation = "w"
|
||||
|
||||
requires_recoloring = self.draw_engine.draw_rounded_progress_bar_with_border(self.apply_widget_scaling(self.current_width),
|
||||
self.apply_widget_scaling(self.current_height),
|
||||
requires_recoloring = self.draw_engine.draw_rounded_progress_bar_with_border(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
self.apply_widget_scaling(self.border_width),
|
||||
self.value,
|
||||
orientation)
|
||||
|
||||
if no_color_updates is False or requires_recoloring:
|
||||
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.canvas.itemconfig("border_parts",
|
||||
fill=ThemeManager.single_color(self.border_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.border_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
fill=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("progress_parts",
|
||||
fill=ThemeManager.single_color(self.progress_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.progress_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.progress_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.progress_color, self._appearance_mode))
|
||||
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
@ -159,6 +165,14 @@ class CTkProgressBar(CTkBaseClass):
|
||||
|
||||
del kwargs["variable"]
|
||||
|
||||
if "width" in kwargs:
|
||||
self.set_dimensions(width=kwargs["width"])
|
||||
del kwargs["width"]
|
||||
|
||||
if "height" in kwargs:
|
||||
self.set_dimensions(height=kwargs["height"])
|
||||
del kwargs["height"]
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
if require_redraw is True:
|
||||
|
@ -1,5 +1,6 @@
|
||||
import tkinter
|
||||
import sys
|
||||
from typing import Callable, Union
|
||||
|
||||
from .ctk_canvas import CTkCanvas
|
||||
from ..theme_manager import ThemeManager
|
||||
@ -31,7 +32,7 @@ class CTkRadioButton(CTkBaseClass):
|
||||
textvariable=None,
|
||||
**kwargs):
|
||||
|
||||
# transfer basic functionality (bg_color, size, appearance_mode, scaling) to CTkBaseClass
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
|
||||
|
||||
# color
|
||||
@ -47,7 +48,7 @@ class CTkRadioButton(CTkBaseClass):
|
||||
|
||||
# text
|
||||
self.text = text
|
||||
self.text_label = None
|
||||
self.text_label: Union[tkinter.Label, None] = None
|
||||
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
||||
self.text_color_disabled = ThemeManager.theme["color"]["text_disabled"] if text_color_disabled == "default_theme" else text_color_disabled
|
||||
self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
|
||||
@ -70,24 +71,23 @@ class CTkRadioButton(CTkBaseClass):
|
||||
|
||||
self.bg_canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self.current_width),
|
||||
height=self.apply_widget_scaling(self.current_height))
|
||||
width=self.apply_widget_scaling(self._current_width),
|
||||
height=self.apply_widget_scaling(self._current_height))
|
||||
self.bg_canvas.grid(row=0, column=0, padx=0, pady=0, columnspan=3, rowspan=1, sticky="nswe")
|
||||
|
||||
self.canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self.current_width),
|
||||
height=self.apply_widget_scaling(self.current_height))
|
||||
width=self.apply_widget_scaling(self._current_width),
|
||||
height=self.apply_widget_scaling(self._current_height))
|
||||
self.canvas.grid(row=0, column=0, padx=0, pady=0, columnspan=1)
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
self.canvas.bind("<Enter>", self.on_enter)
|
||||
self.canvas.bind("<Leave>", self.on_leave)
|
||||
self.canvas.bind("<Button-1>", self.invoke)
|
||||
self.canvas.bind("<Button-1>", self.invoke)
|
||||
|
||||
self.set_cursor()
|
||||
self.draw() # initial draw
|
||||
self.set_cursor()
|
||||
|
||||
if self.variable is not None:
|
||||
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
||||
@ -102,8 +102,8 @@ class CTkRadioButton(CTkBaseClass):
|
||||
self.grid_columnconfigure(1, weight=0, minsize=self.apply_widget_scaling(6))
|
||||
self.text_label.configure(font=self.apply_font_scaling(self.text_font))
|
||||
|
||||
self.bg_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.bg_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()
|
||||
|
||||
def destroy(self):
|
||||
@ -113,26 +113,26 @@ class CTkRadioButton(CTkBaseClass):
|
||||
super().destroy()
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self.current_width),
|
||||
self.apply_widget_scaling(self.current_height),
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
self.apply_widget_scaling(self.border_width))
|
||||
|
||||
self.bg_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.bg_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))
|
||||
|
||||
if self.check_state is False:
|
||||
self.canvas.itemconfig("border_parts",
|
||||
outline=ThemeManager.single_color(self.border_color, self.appearance_mode),
|
||||
fill=ThemeManager.single_color(self.border_color, self.appearance_mode))
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("border_parts",
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
fill=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
outline=ThemeManager.single_color(self.bg_color, self.appearance_mode),
|
||||
fill=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
|
||||
if self.text_label is None:
|
||||
self.text_label = tkinter.Label(master=self,
|
||||
@ -143,12 +143,16 @@ class CTkRadioButton(CTkBaseClass):
|
||||
self.text_label.grid(row=0, column=2, padx=0, pady=0, sticky="w")
|
||||
self.text_label["anchor"] = "w"
|
||||
|
||||
if self.state == tkinter.DISABLED:
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color_disabled, self.appearance_mode))
|
||||
else:
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self.appearance_mode))
|
||||
self.text_label.bind("<Enter>", self.on_enter)
|
||||
self.text_label.bind("<Leave>", self.on_leave)
|
||||
self.text_label.bind("<Button-1>", self.invoke)
|
||||
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
if self.state == tkinter.DISABLED:
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color_disabled, self._appearance_mode))
|
||||
else:
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
|
||||
self.set_text(self.text)
|
||||
|
||||
@ -229,14 +233,22 @@ class CTkRadioButton(CTkBaseClass):
|
||||
if self.state == tkinter.DISABLED:
|
||||
if sys.platform == "darwin" and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="arrow")
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="arrow")
|
||||
elif sys.platform.startswith("win") and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="arrow")
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="arrow")
|
||||
|
||||
elif self.state == tkinter.NORMAL:
|
||||
if sys.platform == "darwin" and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="pointinghand")
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="pointinghand")
|
||||
elif sys.platform.startswith("win") and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="hand2")
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="hand2")
|
||||
|
||||
def set_text(self, text):
|
||||
self.text = text
|
||||
@ -248,19 +260,19 @@ class CTkRadioButton(CTkBaseClass):
|
||||
def on_enter(self, event=0):
|
||||
if self.hover is True and self.state == tkinter.NORMAL:
|
||||
self.canvas.itemconfig("border_parts",
|
||||
fill=ThemeManager.single_color(self.hover_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.hover_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.hover_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.hover_color, self._appearance_mode))
|
||||
|
||||
def on_leave(self, event=0):
|
||||
if self.hover is True:
|
||||
if self.check_state is True:
|
||||
self.canvas.itemconfig("border_parts",
|
||||
fill=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("border_parts",
|
||||
fill=ThemeManager.single_color(self.border_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.border_color, self.appearance_mode))
|
||||
fill=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
|
||||
def variable_callback(self, var_name, index, mode):
|
||||
if not self.variable_callback_blocked:
|
||||
|
@ -9,7 +9,7 @@ from .widget_base_class import CTkBaseClass
|
||||
|
||||
|
||||
class CTkSlider(CTkBaseClass):
|
||||
""" tkinter custom slider, always horizontal """
|
||||
""" tkinter custom slider"""
|
||||
|
||||
def __init__(self, *args,
|
||||
bg_color=None,
|
||||
@ -44,7 +44,7 @@ class CTkSlider(CTkBaseClass):
|
||||
else:
|
||||
height = 16
|
||||
|
||||
# transfer basic functionality (bg_color, size, appearance_mode, scaling) to CTkBaseClass
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
|
||||
|
||||
# color
|
||||
@ -81,8 +81,8 @@ class CTkSlider(CTkBaseClass):
|
||||
|
||||
self.canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self.desired_width),
|
||||
height=self.apply_widget_scaling(self.desired_height))
|
||||
width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.canvas.grid(column=0, row=0, rowspan=1, columnspan=1, sticky="nswe")
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
@ -106,7 +106,14 @@ class CTkSlider(CTkBaseClass):
|
||||
def set_scaling(self, *args, **kwargs):
|
||||
super().set_scaling(*args, **kwargs)
|
||||
|
||||
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()
|
||||
|
||||
def set_dimensions(self, width=None, height=None):
|
||||
super().set_dimensions(width, height)
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
|
||||
def destroy(self):
|
||||
@ -131,8 +138,8 @@ class CTkSlider(CTkBaseClass):
|
||||
else:
|
||||
orientation = "w"
|
||||
|
||||
requires_recoloring = self.draw_engine.draw_rounded_slider_with_border_and_button(self.apply_widget_scaling(self.current_width),
|
||||
self.apply_widget_scaling(self.current_height),
|
||||
requires_recoloring = self.draw_engine.draw_rounded_slider_with_border_and_button(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
self.apply_widget_scaling(self.border_width),
|
||||
self.apply_widget_scaling(self.button_length),
|
||||
@ -140,33 +147,33 @@ class CTkSlider(CTkBaseClass):
|
||||
self.value, orientation)
|
||||
|
||||
if no_color_updates is False or requires_recoloring:
|
||||
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))
|
||||
|
||||
if self.border_color is None:
|
||||
self.canvas.itemconfig("border_parts", fill=ThemeManager.single_color(self.bg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("border_parts", fill=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("border_parts", fill=ThemeManager.single_color(self.border_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.border_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("border_parts", fill=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
|
||||
self.canvas.itemconfig("inner_parts", fill=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("inner_parts", fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
if self.progress_color is None:
|
||||
self.canvas.itemconfig("progress_parts", fill=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("progress_parts", fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("progress_parts", fill=ThemeManager.single_color(self.progress_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.progress_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("progress_parts", fill=ThemeManager.single_color(self.progress_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.progress_color, self._appearance_mode))
|
||||
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
|
||||
def clicked(self, event=None):
|
||||
if self.orient.lower() == "horizontal":
|
||||
self.value = (event.x / self.current_width) / self.widget_scaling
|
||||
self.value = (event.x / self._current_width) / self._widget_scaling
|
||||
else:
|
||||
self.value = 1 - (event.y / self.current_height) / self.widget_scaling
|
||||
self.value = 1 - (event.y / self._current_height) / self._widget_scaling
|
||||
|
||||
if self.value > 1:
|
||||
self.value = 1
|
||||
@ -188,13 +195,13 @@ class CTkSlider(CTkBaseClass):
|
||||
|
||||
def on_enter(self, event=0):
|
||||
self.hover_state = True
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_hover_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_hover_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
|
||||
|
||||
def on_leave(self, event=0):
|
||||
self.hover_state = False
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
|
||||
def round_to_step_size(self, value):
|
||||
if self.number_of_steps is not None:
|
||||
@ -310,6 +317,14 @@ class CTkSlider(CTkBaseClass):
|
||||
|
||||
del kwargs["variable"]
|
||||
|
||||
if "width" in kwargs:
|
||||
self.set_dimensions(width=kwargs["width"])
|
||||
del kwargs["width"]
|
||||
|
||||
if "height" in kwargs:
|
||||
self.set_dimensions(height=kwargs["height"])
|
||||
del kwargs["height"]
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
if require_redraw:
|
||||
|
@ -13,6 +13,7 @@ class CTkSwitch(CTkBaseClass):
|
||||
text="CTkSwitch",
|
||||
text_font="default_theme",
|
||||
text_color="default_theme",
|
||||
text_color_disabled="default_theme",
|
||||
bg_color=None,
|
||||
border_color=None,
|
||||
fg_color="default_theme",
|
||||
@ -30,9 +31,10 @@ class CTkSwitch(CTkBaseClass):
|
||||
offvalue=0,
|
||||
variable=None,
|
||||
textvariable=None,
|
||||
state=tkinter.NORMAL,
|
||||
**kwargs):
|
||||
|
||||
# transfer basic functionality (bg_color, size, appearance_mode, scaling) to CTkBaseClass
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
|
||||
|
||||
# color
|
||||
@ -42,6 +44,7 @@ class CTkSwitch(CTkBaseClass):
|
||||
self.button_color = ThemeManager.theme["color"]["switch_button"] if button_color == "default_theme" else button_color
|
||||
self.button_hover_color = ThemeManager.theme["color"]["switch_button_hover"] if button_hover_color == "default_theme" else button_hover_color
|
||||
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
||||
self.text_color_disabled = ThemeManager.theme["color"]["text_button_disabled"] if text_color_disabled == "default_theme" else text_color_disabled
|
||||
|
||||
# text
|
||||
self.text = text
|
||||
@ -55,6 +58,7 @@ class CTkSwitch(CTkBaseClass):
|
||||
self.button_length = ThemeManager.theme["shape"]["switch_button_length"] if button_length == "default_theme" else button_length
|
||||
self.hover_state = False
|
||||
self.check_state = False # True if switch is activated
|
||||
self.state = state
|
||||
self.onvalue = onvalue
|
||||
self.offvalue = offvalue
|
||||
|
||||
@ -75,14 +79,14 @@ class CTkSwitch(CTkBaseClass):
|
||||
|
||||
self.bg_canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self.current_width),
|
||||
height=self.apply_widget_scaling(self.current_height))
|
||||
width=self.apply_widget_scaling(self._current_width),
|
||||
height=self.apply_widget_scaling(self._current_height))
|
||||
self.bg_canvas.grid(row=0, column=0, padx=0, pady=0, columnspan=3, rowspan=1, sticky="nswe")
|
||||
|
||||
self.canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self.current_width),
|
||||
height=self.apply_widget_scaling(self.current_height))
|
||||
width=self.apply_widget_scaling(self._current_width),
|
||||
height=self.apply_widget_scaling(self._current_height))
|
||||
self.canvas.grid(row=0, column=0, padx=0, pady=0, columnspan=1, sticky="nswe")
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
@ -90,8 +94,8 @@ class CTkSwitch(CTkBaseClass):
|
||||
self.canvas.bind("<Leave>", self.on_leave)
|
||||
self.canvas.bind("<Button-1>", self.toggle)
|
||||
|
||||
self.set_cursor()
|
||||
self.draw() # initial draw
|
||||
self.set_cursor()
|
||||
|
||||
if self.variable is not None:
|
||||
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
||||
@ -106,8 +110,8 @@ class CTkSwitch(CTkBaseClass):
|
||||
self.grid_columnconfigure(1, weight=0, minsize=self.apply_widget_scaling(6))
|
||||
self.text_label.configure(font=self.apply_font_scaling(self.text_font))
|
||||
|
||||
self.bg_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.bg_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()
|
||||
|
||||
def destroy(self):
|
||||
@ -119,24 +123,39 @@ class CTkSwitch(CTkBaseClass):
|
||||
|
||||
def set_cursor(self):
|
||||
if Settings.cursor_manipulation_enabled:
|
||||
if sys.platform == "darwin" and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="pointinghand")
|
||||
elif sys.platform.startswith("win") and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="hand2")
|
||||
if self.state == tkinter.DISABLED:
|
||||
if sys.platform == "darwin" and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="arrow")
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="arrow")
|
||||
elif sys.platform.startswith("win") and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="arrow")
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="arrow")
|
||||
|
||||
elif self.state == tkinter.NORMAL:
|
||||
if sys.platform == "darwin" and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="pointinghand")
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="pointinghand")
|
||||
elif sys.platform.startswith("win") and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="hand2")
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="hand2")
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
|
||||
if self.check_state is True:
|
||||
requires_recoloring = self.draw_engine.draw_rounded_slider_with_border_and_button(self.apply_widget_scaling(self.current_width),
|
||||
self.apply_widget_scaling(self.current_height),
|
||||
requires_recoloring = self.draw_engine.draw_rounded_slider_with_border_and_button(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
self.apply_widget_scaling(self.border_width),
|
||||
self.apply_widget_scaling(self.button_length),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
1, "w")
|
||||
else:
|
||||
requires_recoloring = self.draw_engine.draw_rounded_slider_with_border_and_button(self.apply_widget_scaling(self.current_width),
|
||||
self.apply_widget_scaling(self.current_height),
|
||||
requires_recoloring = self.draw_engine.draw_rounded_slider_with_border_and_button(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
self.apply_widget_scaling(self.border_width),
|
||||
self.apply_widget_scaling(self.button_length),
|
||||
@ -144,28 +163,28 @@ class CTkSwitch(CTkBaseClass):
|
||||
0, "w")
|
||||
|
||||
if no_color_updates is False or requires_recoloring:
|
||||
self.bg_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.bg_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))
|
||||
|
||||
if self.border_color is None:
|
||||
self.canvas.itemconfig("border_parts", fill=ThemeManager.single_color(self.bg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("border_parts", fill=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("border_parts", fill=ThemeManager.single_color(self.border_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.border_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("border_parts", fill=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
|
||||
self.canvas.itemconfig("inner_parts", fill=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("inner_parts", fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
if self.progress_color is None:
|
||||
self.canvas.itemconfig("progress_parts", fill=ThemeManager.single_color(self.fg_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("progress_parts", fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("progress_parts", fill=ThemeManager.single_color(self.progress_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.progress_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("progress_parts", fill=ThemeManager.single_color(self.progress_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.progress_color, self._appearance_mode))
|
||||
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
|
||||
if self.text_label is None:
|
||||
self.text_label = tkinter.Label(master=self,
|
||||
@ -175,11 +194,20 @@ class CTkSwitch(CTkBaseClass):
|
||||
font=self.apply_font_scaling(self.text_font))
|
||||
self.text_label.grid(row=0, column=2, padx=0, pady=0, sticky="w")
|
||||
self.text_label["anchor"] = "w"
|
||||
|
||||
self.text_label.bind("<Enter>", self.on_enter)
|
||||
self.text_label.bind("<Leave>", self.on_leave)
|
||||
self.text_label.bind("<Button-1>", self.toggle)
|
||||
|
||||
if self.textvariable is not None:
|
||||
self.text_label.configure(textvariable=self.textvariable)
|
||||
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self.appearance_mode))
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
if self.state == tkinter.DISABLED:
|
||||
self.text_label.configure(fg=(ThemeManager.single_color(self.text_color_disabled, self._appearance_mode)))
|
||||
else:
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
|
||||
self.set_text(self.text)
|
||||
|
||||
@ -189,59 +217,64 @@ class CTkSwitch(CTkBaseClass):
|
||||
self.text_label.configure(text=self.text)
|
||||
|
||||
def toggle(self, event=None):
|
||||
if self.check_state is True:
|
||||
self.check_state = False
|
||||
else:
|
||||
self.check_state = True
|
||||
if self.state is not tkinter.DISABLED:
|
||||
if self.check_state is True:
|
||||
self.check_state = False
|
||||
else:
|
||||
self.check_state = True
|
||||
|
||||
self.draw(no_color_updates=True)
|
||||
self.draw(no_color_updates=True)
|
||||
|
||||
if self.callback_function is not None:
|
||||
self.callback_function()
|
||||
if self.callback_function is not None:
|
||||
self.callback_function()
|
||||
|
||||
if self.variable is not None:
|
||||
self.variable_callback_blocked = True
|
||||
self.variable.set(self.onvalue if self.check_state is True else self.offvalue)
|
||||
self.variable_callback_blocked = False
|
||||
if self.variable is not None:
|
||||
self.variable_callback_blocked = True
|
||||
self.variable.set(self.onvalue if self.check_state is True else self.offvalue)
|
||||
self.variable_callback_blocked = False
|
||||
|
||||
def select(self, from_variable_callback=False):
|
||||
self.check_state = True
|
||||
if self.state is not tkinter.DISABLED or from_variable_callback:
|
||||
self.check_state = True
|
||||
|
||||
self.draw(no_color_updates=True)
|
||||
self.draw(no_color_updates=True)
|
||||
|
||||
if self.callback_function is not None:
|
||||
self.callback_function()
|
||||
if self.variable is not None and not from_variable_callback:
|
||||
self.variable_callback_blocked = True
|
||||
self.variable.set(self.onvalue)
|
||||
self.variable_callback_blocked = False
|
||||
|
||||
if self.variable is not None and not from_variable_callback:
|
||||
self.variable_callback_blocked = True
|
||||
self.variable.set(self.onvalue)
|
||||
self.variable_callback_blocked = False
|
||||
if self.callback_function is not None:
|
||||
self.callback_function()
|
||||
|
||||
def deselect(self, from_variable_callback=False):
|
||||
self.check_state = False
|
||||
if self.state is not tkinter.DISABLED or from_variable_callback:
|
||||
self.check_state = False
|
||||
|
||||
self.draw(no_color_updates=True)
|
||||
self.draw(no_color_updates=True)
|
||||
|
||||
if self.callback_function is not None:
|
||||
self.callback_function()
|
||||
if self.variable is not None and not from_variable_callback:
|
||||
self.variable_callback_blocked = True
|
||||
self.variable.set(self.offvalue)
|
||||
self.variable_callback_blocked = False
|
||||
|
||||
if self.variable is not None and not from_variable_callback:
|
||||
self.variable_callback_blocked = True
|
||||
self.variable.set(self.offvalue)
|
||||
self.variable_callback_blocked = False
|
||||
if self.callback_function is not None:
|
||||
self.callback_function()
|
||||
|
||||
def get(self):
|
||||
return self.onvalue if self.check_state is True else self.offvalue
|
||||
|
||||
def on_enter(self, event=0):
|
||||
self.hover_state = True
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_hover_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_hover_color, self.appearance_mode))
|
||||
|
||||
if self.state is not tkinter.DISABLED:
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
|
||||
|
||||
def on_leave(self, event=0):
|
||||
self.hover_state = False
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_color, self.appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_color, self.appearance_mode))
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
|
||||
def variable_callback(self, var_name, index, mode):
|
||||
if not self.variable_callback_blocked:
|
||||
@ -253,6 +286,12 @@ class CTkSwitch(CTkBaseClass):
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
|
||||
if "state" in kwargs:
|
||||
self.state = kwargs["state"]
|
||||
self.set_cursor()
|
||||
require_redraw = True
|
||||
del kwargs["state"]
|
||||
|
||||
if "fg_color" in kwargs:
|
||||
self.fg_color = kwargs["fg_color"]
|
||||
require_redraw = True
|
||||
|
139
customtkinter/widgets/dropdown_menu.py
Normal file
139
customtkinter/widgets/dropdown_menu.py
Normal file
@ -0,0 +1,139 @@
|
||||
import customtkinter
|
||||
import tkinter
|
||||
import sys
|
||||
from typing import Union
|
||||
|
||||
from ..theme_manager import ThemeManager
|
||||
from ..appearance_mode_tracker import AppearanceModeTracker
|
||||
from ..scaling_tracker import ScalingTracker
|
||||
|
||||
|
||||
class DropdownMenu(tkinter.Toplevel):
|
||||
def __init__(self, *args,
|
||||
fg_color="#555555",
|
||||
button_color="gray50",
|
||||
button_hover_color="gray35",
|
||||
text_color="black",
|
||||
corner_radius=6,
|
||||
button_corner_radius=3,
|
||||
width=120,
|
||||
button_height=24,
|
||||
x_position=0,
|
||||
y_position=0,
|
||||
x_spacing=3,
|
||||
y_spacing=3,
|
||||
command=None,
|
||||
values=None,
|
||||
**kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
ScalingTracker.add_widget(self.set_scaling, self)
|
||||
self._widget_scaling = ScalingTracker.get_widget_scaling(self)
|
||||
self._spacing_scaling = ScalingTracker.get_spacing_scaling(self)
|
||||
|
||||
self.values = values
|
||||
self.command = command
|
||||
|
||||
# color
|
||||
self.appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
||||
self.fg_color = fg_color
|
||||
self.button_color = button_color
|
||||
self.button_hover_color = button_hover_color
|
||||
self.text_color = text_color
|
||||
|
||||
# shape
|
||||
self.corner_radius = corner_radius
|
||||
self.button_corner_radius = button_corner_radius
|
||||
self.button_height = button_height
|
||||
self.width = width
|
||||
self.height = max(len(self.values), 1) * (self.button_height + self.apply_spacing_scaling(y_spacing)) + self.apply_spacing_scaling(y_spacing)
|
||||
|
||||
self.geometry(f"{round(self.apply_widget_scaling(self.width))}x" +
|
||||
f"{round(self.apply_widget_scaling(self.height))}+" +
|
||||
f"{round(x_position)}+{round(y_position)}")
|
||||
self.grid_columnconfigure(0, weight=1)
|
||||
self.grid_rowconfigure(0, weight=1)
|
||||
|
||||
if sys.platform.startswith("darwin"):
|
||||
self.overrideredirect(True) # remove title-bar
|
||||
self.overrideredirect(False)
|
||||
self.wm_attributes("-transparent", True) # turn off window shadow
|
||||
self.config(bg='systemTransparent') # transparent bg
|
||||
self.frame = customtkinter.CTkFrame(self,
|
||||
border_width=0,
|
||||
width=self.width,
|
||||
corner_radius=self.corner_radius,
|
||||
fg_color=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
|
||||
elif sys.platform.startswith("win"):
|
||||
self.overrideredirect(True) # remove title-bar
|
||||
self.configure(bg="#010302")
|
||||
self.wm_attributes("-transparentcolor", "#010302")
|
||||
self.focus()
|
||||
self.frame = customtkinter.CTkFrame(self,
|
||||
border_width=0,
|
||||
width=self.width,
|
||||
corner_radius=self.corner_radius,
|
||||
fg_color=self.fg_color, overwrite_preferred_drawing_method="circle_shapes")
|
||||
else:
|
||||
self.overrideredirect(True) # remove title-bar
|
||||
self.configure(bg="#010302")
|
||||
self.wm_attributes("-transparentcolor", "#010302")
|
||||
self.frame = customtkinter.CTkFrame(self,
|
||||
border_width=0,
|
||||
width=self.width,
|
||||
corner_radius=self.corner_radius,
|
||||
fg_color=self.fg_color, overwrite_preferred_drawing_method="circle_shapes")
|
||||
|
||||
self.frame.grid(row=0, column=0, sticky="nsew", rowspan=1)
|
||||
self.frame.grid_rowconfigure(len(self.values) + 1, minsize=self.apply_spacing_scaling(y_spacing)) # add spacing at the bottom
|
||||
self.frame.grid_columnconfigure(0, weight=1)
|
||||
|
||||
self.button_list = []
|
||||
for index, option in enumerate(self.values):
|
||||
button = customtkinter.CTkButton(self.frame,
|
||||
text=option,
|
||||
height=self.button_height,
|
||||
width=self.width - 2 * self.apply_widget_scaling(x_spacing),
|
||||
fg_color=self.button_color,
|
||||
text_color=self.text_color,
|
||||
hover_color=self.button_hover_color,
|
||||
corner_radius=self.button_corner_radius,
|
||||
command=lambda i=index: self.button_callback(i))
|
||||
button.text_label.configure(anchor="w")
|
||||
button.text_label.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="w")
|
||||
button.grid(row=index, column=0,
|
||||
padx=self.apply_widget_scaling(x_spacing),
|
||||
pady=(self.apply_widget_scaling(y_spacing), 0), sticky="ew")
|
||||
self.button_list.append(button)
|
||||
|
||||
self.bind("<FocusOut>", self.focus_loss_event)
|
||||
self.frame.canvas.bind("<Button-1>", self.focus_loss_event)
|
||||
|
||||
def apply_widget_scaling(self, value: Union[int, float, str]) -> Union[float, str]:
|
||||
if isinstance(value, (int, float)):
|
||||
return value * self._widget_scaling
|
||||
else:
|
||||
return value
|
||||
|
||||
def apply_spacing_scaling(self, value: Union[int, float, str]) -> Union[float, str]:
|
||||
if isinstance(value, (int, float)):
|
||||
return value * self._spacing_scaling
|
||||
else:
|
||||
return value
|
||||
|
||||
def set_scaling(self, new_widget_scaling, new_spacing_scaling, new_window_scaling):
|
||||
return
|
||||
|
||||
def focus_loss_event(self, event):
|
||||
self.destroy()
|
||||
if sys.platform.startswith("darwin"):
|
||||
self.update()
|
||||
|
||||
def button_callback(self, index):
|
||||
self.destroy()
|
||||
if sys.platform.startswith("darwin"):
|
||||
self.update()
|
||||
|
||||
if self.command is not None:
|
||||
self.command(self.values[index])
|
@ -2,7 +2,12 @@ import tkinter
|
||||
import tkinter.ttk as ttk
|
||||
import copy
|
||||
import re
|
||||
from typing import Callable, Union, TypedDict
|
||||
from typing import Callable, Union
|
||||
|
||||
try:
|
||||
from typing import TypedDict
|
||||
except ImportError:
|
||||
from typing_extensions import TypedDict
|
||||
|
||||
from ..windows.ctk_tk import CTk
|
||||
from ..windows.ctk_toplevel import CTkToplevel
|
||||
@ -12,38 +17,48 @@ from ..theme_manager import ThemeManager
|
||||
|
||||
|
||||
class CTkBaseClass(tkinter.Frame):
|
||||
def __init__(self, *args, bg_color=None, width, height, **kwargs):
|
||||
""" Base class of every Ctk widget, handles the dimensions, bg_color,
|
||||
appearance_mode changes, scaling, bg changes of master if master is not a CTk widget """
|
||||
|
||||
def __init__(self,
|
||||
*args,
|
||||
bg_color: Union[str, tuple] = None,
|
||||
width: int,
|
||||
height: int,
|
||||
**kwargs):
|
||||
super().__init__(*args, width=width, height=height, **kwargs) # set desired size of underlying tkinter.Frame
|
||||
|
||||
self.bg_color = self.detect_color_of_master() if bg_color is None else bg_color
|
||||
# dimensions
|
||||
self._current_width = width # _current_width and _current_height in pixel, represent current size of the widget
|
||||
self._current_height = height # _current_width and _current_height are independent of the scale
|
||||
self._desired_width = width # _desired_width and _desired_height, represent desired size set by width and height
|
||||
self._desired_height = height
|
||||
|
||||
self.current_width = width # current_width and current_height in pixel, represent current size of the widget (not the desired size by init)
|
||||
self.current_height = height # current_width and current_height are independent of the scale
|
||||
self.desired_width = width
|
||||
self.desired_height = height
|
||||
# scaling
|
||||
ScalingTracker.add_widget(self.set_scaling, self) # add callback for automatic scaling changes
|
||||
self._widget_scaling = ScalingTracker.get_widget_scaling(self)
|
||||
self._spacing_scaling = ScalingTracker.get_spacing_scaling(self)
|
||||
|
||||
# add set_scaling method to callback list of ScalingTracker for automatic scaling changes
|
||||
ScalingTracker.add_widget(self.set_scaling, self)
|
||||
self.widget_scaling = ScalingTracker.get_widget_scaling(self)
|
||||
self.spacing_scaling = ScalingTracker.get_spacing_scaling(self)
|
||||
|
||||
super().configure(width=self.apply_widget_scaling(self.desired_width),
|
||||
height=self.apply_widget_scaling(self.desired_height))
|
||||
super().configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
|
||||
# save latest geometry function and kwargs
|
||||
class GeometryCallDict(TypedDict):
|
||||
function: Callable
|
||||
kwargs: dict
|
||||
|
||||
self.last_geometry_manager_call: Union[GeometryCallDict, None] = None
|
||||
self._last_geometry_manager_call: Union[GeometryCallDict, None] = None
|
||||
|
||||
# add set_appearance_mode method to callback list of AppearanceModeTracker for appearance mode changes
|
||||
AppearanceModeTracker.add(self.set_appearance_mode, self)
|
||||
self.appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
||||
self._appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
||||
|
||||
super().configure(bg=ThemeManager.single_color(self.bg_color, self.appearance_mode))
|
||||
# background color
|
||||
self.bg_color = self.detect_color_of_master() if bg_color is None else bg_color
|
||||
|
||||
# overwrite configure methods of master when master is tkinter widget, so that bg changes get applied on child CTk widget too
|
||||
super().configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
|
||||
# overwrite configure methods of master when master is tkinter widget, so that bg changes get applied on child CTk widget as well
|
||||
if isinstance(self.master, (tkinter.Tk, tkinter.Toplevel, tkinter.Frame)) and not isinstance(self.master, (CTkBaseClass, CTk, CTkToplevel)):
|
||||
master_old_configure = self.master.config
|
||||
|
||||
@ -69,15 +84,15 @@ class CTkBaseClass(tkinter.Frame):
|
||||
super().destroy()
|
||||
|
||||
def place(self, **kwargs):
|
||||
self.last_geometry_manager_call = {"function": super().place, "kwargs": kwargs}
|
||||
self._last_geometry_manager_call = {"function": super().place, "kwargs": kwargs}
|
||||
super().place(**self.apply_argument_scaling(kwargs))
|
||||
|
||||
def pack(self, **kwargs):
|
||||
self.last_geometry_manager_call = {"function": super().pack, "kwargs": kwargs}
|
||||
self._last_geometry_manager_call = {"function": super().pack, "kwargs": kwargs}
|
||||
super().pack(**self.apply_argument_scaling(kwargs))
|
||||
|
||||
def grid(self, **kwargs):
|
||||
self.last_geometry_manager_call = {"function": super().grid, "kwargs": kwargs}
|
||||
self._last_geometry_manager_call = {"function": super().grid, "kwargs": kwargs}
|
||||
super().grid(**self.apply_argument_scaling(kwargs))
|
||||
|
||||
def apply_argument_scaling(self, kwargs: dict) -> dict:
|
||||
@ -124,36 +139,44 @@ class CTkBaseClass(tkinter.Frame):
|
||||
|
||||
def update_dimensions_event(self, event):
|
||||
# only redraw if dimensions changed (for performance)
|
||||
if round(self.current_width) != round(event.width / self.widget_scaling) or round(self.current_height) != round(event.height / self.widget_scaling):
|
||||
self.current_width = (event.width / self.widget_scaling) # adjust current size according to new size given by event
|
||||
self.current_height = (event.height / self.widget_scaling) # current_width and current_height are independent of the scale
|
||||
if round(self._current_width) != round(event.width / self._widget_scaling) or round(self._current_height) != round(event.height / self._widget_scaling):
|
||||
self._current_width = (event.width / self._widget_scaling) # adjust current size according to new size given by event
|
||||
self._current_height = (event.height / self._widget_scaling) # _current_width and _current_height are independent of the scale
|
||||
|
||||
self.draw(no_color_updates=True) # faster drawing without color changes
|
||||
|
||||
def detect_color_of_master(self):
|
||||
def detect_color_of_master(self, master_widget=None):
|
||||
""" detect color of self.master widget to set correct bg_color """
|
||||
|
||||
if isinstance(self.master, CTkBaseClass) and hasattr(self.master, "fg_color"): # master is CTkFrame
|
||||
return self.master.fg_color
|
||||
if master_widget is None:
|
||||
master_widget = self.master
|
||||
|
||||
elif isinstance(self.master, (ttk.Frame, ttk.LabelFrame, ttk.Notebook)): # master is ttk widget
|
||||
if isinstance(master_widget, CTkBaseClass) and hasattr(master_widget, "fg_color"): # master is CTkFrame
|
||||
if master_widget.fg_color is not None:
|
||||
return master_widget.fg_color
|
||||
|
||||
# if fg_color of master is None, try to retrieve fg_color from master of master
|
||||
elif hasattr(master_widget.master, "master"):
|
||||
return self.detect_color_of_master(self.master.master)
|
||||
|
||||
elif isinstance(master_widget, (ttk.Frame, ttk.LabelFrame, ttk.Notebook)): # master is ttk widget
|
||||
try:
|
||||
ttk_style = ttk.Style()
|
||||
return ttk_style.lookup(self.master.winfo_class(), 'background')
|
||||
return ttk_style.lookup(master_widget.winfo_class(), 'background')
|
||||
except Exception:
|
||||
return "#FFFFFF", "#000000"
|
||||
|
||||
else: # master is normal tkinter widget
|
||||
try:
|
||||
return self.master.cget("bg") # try to get bg color by .cget() method
|
||||
return master_widget.cget("bg") # try to get bg color by .cget() method
|
||||
except Exception:
|
||||
return "#FFFFFF", "#000000"
|
||||
|
||||
def set_appearance_mode(self, mode_string):
|
||||
if mode_string.lower() == "dark":
|
||||
self.appearance_mode = 1
|
||||
self._appearance_mode = 1
|
||||
elif mode_string.lower() == "light":
|
||||
self.appearance_mode = 0
|
||||
self._appearance_mode = 0
|
||||
|
||||
if isinstance(self.master, (CTkBaseClass, CTk)) and hasattr(self.master, "fg_color"):
|
||||
self.bg_color = self.master.fg_color
|
||||
@ -163,24 +186,33 @@ class CTkBaseClass(tkinter.Frame):
|
||||
self.draw()
|
||||
|
||||
def set_scaling(self, new_widget_scaling, new_spacing_scaling, new_window_scaling):
|
||||
self.widget_scaling = new_widget_scaling
|
||||
self.spacing_scaling = new_spacing_scaling
|
||||
self._widget_scaling = new_widget_scaling
|
||||
self._spacing_scaling = new_spacing_scaling
|
||||
|
||||
super().configure(width=self.apply_widget_scaling(self.desired_width),
|
||||
height=self.apply_widget_scaling(self.desired_height))
|
||||
super().configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
|
||||
if self.last_geometry_manager_call is not None:
|
||||
self.last_geometry_manager_call["function"](**self.apply_argument_scaling(self.last_geometry_manager_call["kwargs"]))
|
||||
if self._last_geometry_manager_call is not None:
|
||||
self._last_geometry_manager_call["function"](**self.apply_argument_scaling(self._last_geometry_manager_call["kwargs"]))
|
||||
|
||||
def apply_widget_scaling(self, value):
|
||||
def set_dimensions(self, width=None, height=None):
|
||||
if width is not None:
|
||||
self._desired_width = width
|
||||
if height is not None:
|
||||
self._desired_height = height
|
||||
|
||||
super().configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
|
||||
def apply_widget_scaling(self, value: Union[int, float, str]) -> Union[float, str]:
|
||||
if isinstance(value, (int, float)):
|
||||
return value * self.widget_scaling
|
||||
return value * self._widget_scaling
|
||||
else:
|
||||
return value
|
||||
|
||||
def apply_spacing_scaling(self, value):
|
||||
def apply_spacing_scaling(self, value: Union[int, float, str]) -> Union[float, str]:
|
||||
if isinstance(value, (int, float)):
|
||||
return value * self.spacing_scaling
|
||||
return value * self._spacing_scaling
|
||||
else:
|
||||
return value
|
||||
|
||||
@ -189,25 +221,23 @@ class CTkBaseClass(tkinter.Frame):
|
||||
font_list = list(font)
|
||||
for i in range(len(font_list)):
|
||||
if (type(font_list[i]) == int or type(font_list[i]) == float) and font_list[i] < 0:
|
||||
font_list[i] = int(font_list[i] * self.widget_scaling)
|
||||
font_list[i] = int(font_list[i] * self._widget_scaling)
|
||||
return tuple(font_list)
|
||||
|
||||
elif type(font) == str:
|
||||
for negative_number in re.findall(r" -\d* ", font):
|
||||
font = font.replace(negative_number, f" {int(int(negative_number) * self.widget_scaling)} ")
|
||||
font = font.replace(negative_number, f" {int(int(negative_number) * self._widget_scaling)} ")
|
||||
return font
|
||||
|
||||
elif isinstance(font, tkinter.font.Font):
|
||||
new_font_object = copy.copy(font)
|
||||
if font.cget("size") < 0:
|
||||
new_font_object.config(size=int(font.cget("size") * self.widget_scaling))
|
||||
new_font_object.config(size=int(font.cget("size") * self._widget_scaling))
|
||||
return new_font_object
|
||||
|
||||
else:
|
||||
return font
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
def draw(self, no_color_updates: bool = False):
|
||||
""" abstract of draw method to be overridden """
|
||||
pass
|
||||
|
||||
|
||||
|
@ -68,8 +68,7 @@ class CTk(tkinter.Tk):
|
||||
|
||||
if self.current_width != round(detected_width / self.window_scaling) or self.current_height != round(detected_height / self.window_scaling):
|
||||
self.current_width = round(detected_width / self.window_scaling) # adjust current size according to new size given by event
|
||||
self.current_height = round(detected_height / self.window_scaling) # current_width and current_height are independent of the scale
|
||||
print("update_dimensions_event:", self.current_width)
|
||||
self.current_height = round(detected_height / self.window_scaling) # _current_width and _current_height are independent of the scale
|
||||
|
||||
def set_scaling(self, new_widget_scaling, new_spacing_scaling, new_window_scaling):
|
||||
self.window_scaling = new_window_scaling
|
||||
@ -78,7 +77,6 @@ class CTk(tkinter.Tk):
|
||||
super().minsize(self.apply_window_scaling(self.current_width), self.apply_window_scaling(self.current_height))
|
||||
super().maxsize(self.apply_window_scaling(self.current_width), self.apply_window_scaling(self.current_height))
|
||||
super().geometry(f"{self.apply_window_scaling(self.current_width)}x"+f"{self.apply_window_scaling(self.current_height)}")
|
||||
print("set_scaling:", self.apply_window_scaling(self.current_width), self.max_width, self.min_width)
|
||||
|
||||
# set new scaled min and max with 400ms delay (otherwise it won't work for some reason)
|
||||
self.after(400, self.set_scaled_min_max)
|
||||
@ -103,7 +101,6 @@ class CTk(tkinter.Tk):
|
||||
|
||||
def mainloop(self, *args, **kwargs):
|
||||
if not self.window_exists:
|
||||
print("deiconify")
|
||||
self.deiconify()
|
||||
self.window_exists = True
|
||||
super().mainloop(*args, **kwargs)
|
||||
@ -133,7 +130,6 @@ class CTk(tkinter.Tk):
|
||||
super().maxsize(self.apply_window_scaling(self.max_width), self.apply_window_scaling(self.max_height))
|
||||
|
||||
def geometry(self, geometry_string):
|
||||
print("geometry:", geometry_string)
|
||||
super().geometry(self.apply_geometry_scaling(geometry_string))
|
||||
|
||||
# update width and height attributes
|
||||
@ -142,18 +138,7 @@ class CTk(tkinter.Tk):
|
||||
self.current_height = max(self.min_height, min(numbers[1], self.max_height))
|
||||
|
||||
def apply_geometry_scaling(self, geometry_string):
|
||||
numbers = list(map(int, re.split(r"[x+]", geometry_string))) # split geometry string into list of numbers
|
||||
|
||||
if len(numbers) == 2:
|
||||
return f"{self.apply_window_scaling(numbers[0]):.0f}x" +\
|
||||
f"{self.apply_window_scaling(numbers[1]):.0f}"
|
||||
elif len(numbers) == 4:
|
||||
return f"{self.apply_window_scaling(numbers[0]):.0f}x" +\
|
||||
f"{self.apply_window_scaling(numbers[1]):.0f}+" +\
|
||||
f"{self.apply_window_scaling(numbers[2]):.0f}+" +\
|
||||
f"{self.apply_window_scaling(numbers[3]):.0f}"
|
||||
else:
|
||||
return geometry_string
|
||||
return re.sub(re.compile("\d+"), lambda match_obj: str(round(int(match_obj.group(0)) * self.window_scaling)), geometry_string)
|
||||
|
||||
def apply_window_scaling(self, value):
|
||||
if isinstance(value, (int, float)):
|
||||
|
@ -47,7 +47,7 @@ class CTkToplevel(tkinter.Toplevel):
|
||||
AppearanceModeTracker.add(self.set_appearance_mode, self)
|
||||
super().configure(bg=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
super().title("CTkToplevel")
|
||||
# self.geometry(f"{self.current_width}x{self.current_height}")
|
||||
# self.geometry(f"{self._current_width}x{self._current_height}")
|
||||
|
||||
if sys.platform.startswith("win"):
|
||||
if self.appearance_mode == 1:
|
||||
@ -63,7 +63,7 @@ class CTkToplevel(tkinter.Toplevel):
|
||||
|
||||
if self.current_width != round(detected_width / self.window_scaling) or self.current_height != round(detected_height / self.window_scaling):
|
||||
self.current_width = round(detected_width / self.window_scaling) # adjust current size according to new size given by event
|
||||
self.current_height = round(detected_height / self.window_scaling) # current_width and current_height are independent of the scale
|
||||
self.current_height = round(detected_height / self.window_scaling) # _current_width and _current_height are independent of the scale
|
||||
|
||||
def set_scaling(self, new_widget_scaling, new_spacing_scaling, new_window_scaling):
|
||||
self.window_scaling = new_window_scaling
|
||||
@ -73,7 +73,6 @@ class CTkToplevel(tkinter.Toplevel):
|
||||
super().maxsize(self.apply_window_scaling(self.current_width), self.apply_window_scaling(self.current_height))
|
||||
super().geometry(
|
||||
f"{self.apply_window_scaling(self.current_width)}x" + f"{self.apply_window_scaling(self.current_height)}")
|
||||
print("set_scaling:", self.apply_window_scaling(self.current_width), self.max_width, self.min_width)
|
||||
|
||||
# set new scaled min and max with 400ms delay (otherwise it won't work for some reason)
|
||||
self.after(400, self.set_scaled_min_max)
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 395 KiB After Width: | Height: | Size: 395 KiB |
@ -14,10 +14,8 @@ class App(customtkinter.CTk):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
self.title("CustomTkinter complex example")
|
||||
self.title("CustomTkinter complex_example.py")
|
||||
self.geometry(f"{App.WIDTH}x{App.HEIGHT}")
|
||||
# self.minsize(App.WIDTH, App.HEIGHT)
|
||||
|
||||
self.protocol("WM_DELETE_WINDOW", self.on_closing) # call .on_closing() when app gets closed
|
||||
|
||||
# ============ create two frames ============
|
||||
@ -197,10 +195,7 @@ class App(customtkinter.CTk):
|
||||
def on_closing(self, event=0):
|
||||
self.destroy()
|
||||
|
||||
def start(self):
|
||||
self.mainloop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = App()
|
||||
app.start()
|
||||
app.mainloop()
|
||||
|
@ -12,7 +12,7 @@ PATH = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
class App(customtkinter.CTk):
|
||||
|
||||
APP_NAME = "CustomTkinter background gradient image"
|
||||
APP_NAME = "CustomTkinter example_background_image.py"
|
||||
WIDTH = 900
|
||||
HEIGHT = 600
|
||||
|
||||
@ -23,6 +23,7 @@ class App(customtkinter.CTk):
|
||||
self.geometry(f"{App.WIDTH}x{App.HEIGHT}")
|
||||
self.minsize(App.WIDTH, App.HEIGHT)
|
||||
self.maxsize(App.WIDTH, App.HEIGHT)
|
||||
self.resizable(False, False)
|
||||
|
||||
self.protocol("WM_DELETE_WINDOW", self.on_closing)
|
||||
|
||||
@ -40,13 +41,13 @@ class App(customtkinter.CTk):
|
||||
self.frame.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
|
||||
|
||||
self.label_1 = customtkinter.CTkLabel(master=self.frame, width=200, height=60,
|
||||
fg_color=("gray70", "gray35"), text="CustomTkinter\ninterface example")
|
||||
fg_color=("gray70", "gray25"), text="CustomTkinter\ninterface example")
|
||||
self.label_1.place(relx=0.5, rely=0.3, anchor=tkinter.CENTER)
|
||||
|
||||
self.entry_1 = customtkinter.CTkEntry(master=self.frame, corner_radius=20, width=200, placeholder_text="username")
|
||||
self.entry_1 = customtkinter.CTkEntry(master=self.frame, corner_radius=6, width=200, placeholder_text="username")
|
||||
self.entry_1.place(relx=0.5, rely=0.52, anchor=tkinter.CENTER)
|
||||
|
||||
self.entry_2 = customtkinter.CTkEntry(master=self.frame, corner_radius=20, width=200, show="*", placeholder_text="password")
|
||||
self.entry_2 = customtkinter.CTkEntry(master=self.frame, corner_radius=6, width=200, show="*", placeholder_text="password")
|
||||
self.entry_2.place(relx=0.5, rely=0.6, anchor=tkinter.CENTER)
|
||||
|
||||
self.button_2 = customtkinter.CTkButton(master=self.frame, text="Login",
|
||||
|
@ -1,5 +1,5 @@
|
||||
import tkinter
|
||||
import customtkinter # <- import the CustomTkinter module
|
||||
import customtkinter
|
||||
from PIL import Image, ImageTk # <- import PIL for the images
|
||||
import os
|
||||
|
||||
@ -8,9 +8,9 @@ PATH = os.path.dirname(os.path.realpath(__file__))
|
||||
customtkinter.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light"
|
||||
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
|
||||
|
||||
root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
|
||||
root_tk.geometry("450x260")
|
||||
root_tk.title("CustomTkinter button images")
|
||||
app = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
|
||||
app.geometry("450x260")
|
||||
app.title("CustomTkinter example_button_images.py")
|
||||
|
||||
|
||||
def button_function():
|
||||
@ -29,10 +29,10 @@ add_user_image = ImageTk.PhotoImage(Image.open(PATH + "/test_images/add-user.png
|
||||
chat_image = ImageTk.PhotoImage(Image.open(PATH + "/test_images/chat.png").resize((image_size, image_size), Image.ANTIALIAS))
|
||||
home_image = ImageTk.PhotoImage(Image.open(PATH + "/test_images/home.png").resize((image_size, image_size), Image.ANTIALIAS))
|
||||
|
||||
root_tk.grid_rowconfigure(0, weight=1)
|
||||
root_tk.grid_columnconfigure(0, weight=1, minsize=200)
|
||||
app.grid_rowconfigure(0, weight=1)
|
||||
app.grid_columnconfigure(0, weight=1, minsize=200)
|
||||
|
||||
frame_1 = customtkinter.CTkFrame(master=root_tk, width=250, height=240, corner_radius=15)
|
||||
frame_1 = customtkinter.CTkFrame(master=app, width=250, height=240, corner_radius=15)
|
||||
frame_1.grid(row=0, column=0, padx=20, pady=20, sticky="nsew")
|
||||
|
||||
frame_1.grid_columnconfigure(0, weight=1)
|
||||
@ -56,9 +56,9 @@ button_4 = customtkinter.CTkButton(master=frame_1, image=home_image, text="", wi
|
||||
corner_radius=10, fg_color="gray40", hover_color="gray25", command=button_function)
|
||||
button_4.grid(row=3, column=1, columnspan=1, padx=20, pady=10, sticky="e")
|
||||
|
||||
button_5 = customtkinter.CTkButton(master=root_tk, image=add_user_image, text="Add User", width=130, height=70, border_width=3,
|
||||
button_5 = customtkinter.CTkButton(master=app, image=add_user_image, text="Add User", width=130, height=70, border_width=3,
|
||||
corner_radius=10, compound="bottom", border_color="#D35B58", fg_color=("gray84", "gray25"), hover_color="#C77C78",
|
||||
command=button_function)
|
||||
button_5.grid(row=0, column=1, padx=20, pady=20)
|
||||
|
||||
root_tk.mainloop()
|
||||
app.mainloop()
|
||||
|
@ -1,61 +1,61 @@
|
||||
import tkinter
|
||||
import customtkinter # <- import the CustomTkinter module
|
||||
import customtkinter
|
||||
|
||||
customtkinter.set_appearance_mode("dark") # Modes: "System" (standard), "Dark", "Light"
|
||||
# customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
|
||||
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
|
||||
|
||||
root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
|
||||
root_tk.geometry("400x480")
|
||||
root_tk.title("CustomTkinter Test")
|
||||
app = customtkinter.CTk()
|
||||
app.geometry("400x580")
|
||||
app.title("CustomTkinter simple_example.py")
|
||||
|
||||
|
||||
def button_function():
|
||||
print("Button click", label_1.text_label.cget("text"))
|
||||
def button_callback():
|
||||
print("Button click", combobox_1.get())
|
||||
|
||||
|
||||
def slider_function(value):
|
||||
def slider_callback(value):
|
||||
progressbar_1.set(value)
|
||||
|
||||
|
||||
def check_box_function():
|
||||
print("checkbox_1:", checkbox_1.get())
|
||||
|
||||
|
||||
y_padding = 13
|
||||
|
||||
frame_1 = customtkinter.CTkFrame(master=root_tk, corner_radius=15)
|
||||
frame_1 = customtkinter.CTkFrame(master=app)
|
||||
frame_1.pack(pady=20, padx=60, fill="both", expand=True)
|
||||
|
||||
label_1 = customtkinter.CTkLabel(master=frame_1, justify=tkinter.LEFT)
|
||||
label_1.pack(pady=y_padding, padx=10)
|
||||
label_1.pack(pady=12, padx=10)
|
||||
|
||||
progressbar_1 = customtkinter.CTkProgressBar(master=frame_1)
|
||||
progressbar_1.pack(pady=y_padding, padx=10)
|
||||
progressbar_1.pack(pady=12, padx=10)
|
||||
|
||||
button_1 = customtkinter.CTkButton(master=frame_1, corner_radius=8, command=button_function)
|
||||
button_1.pack(pady=y_padding, padx=10)
|
||||
button_1 = customtkinter.CTkButton(master=frame_1, command=button_callback)
|
||||
button_1.pack(pady=12, padx=10)
|
||||
|
||||
slider_1 = customtkinter.CTkSlider(master=frame_1, command=slider_function, from_=0, to=1)
|
||||
slider_1.pack(pady=y_padding, padx=10)
|
||||
slider_1 = customtkinter.CTkSlider(master=frame_1, command=slider_callback, from_=0, to=1)
|
||||
slider_1.pack(pady=12, padx=10)
|
||||
slider_1.set(0.5)
|
||||
|
||||
entry_1 = customtkinter.CTkEntry(master=frame_1, placeholder_text="CTkEntry")
|
||||
entry_1.pack(pady=y_padding, padx=10)
|
||||
entry_1.pack(pady=12, padx=10)
|
||||
|
||||
checkbox_1 = customtkinter.CTkCheckBox(master=frame_1, command=check_box_function)
|
||||
checkbox_1.pack(pady=y_padding, padx=10)
|
||||
optionmenu_1 = customtkinter.CTkOptionMenu(frame_1, values=["Option 1", "Option 2", "Option 42"])
|
||||
optionmenu_1.pack(pady=12, padx=10)
|
||||
optionmenu_1.set("CTkOptionMenu")
|
||||
|
||||
combobox_1 = customtkinter.CTkComboBox(frame_1, values=["Option 1", "Option 2", "Option 42"])
|
||||
combobox_1.pack(pady=12, padx=10)
|
||||
optionmenu_1.set("CTkComboBox")
|
||||
|
||||
checkbox_1 = customtkinter.CTkCheckBox(master=frame_1)
|
||||
checkbox_1.pack(pady=12, padx=10)
|
||||
|
||||
radiobutton_var = tkinter.IntVar(value=1)
|
||||
|
||||
radiobutton_1 = customtkinter.CTkRadioButton(master=frame_1, variable=radiobutton_var, value=1)
|
||||
radiobutton_1.pack(pady=y_padding, padx=10)
|
||||
radiobutton_1.pack(pady=12, padx=10)
|
||||
|
||||
radiobutton_2 = customtkinter.CTkRadioButton(master=frame_1, variable=radiobutton_var, value=2)
|
||||
radiobutton_2.pack(pady=y_padding, padx=10)
|
||||
|
||||
s_var = tkinter.StringVar(value="on")
|
||||
radiobutton_2.pack(pady=12, padx=10)
|
||||
|
||||
switch_1 = customtkinter.CTkSwitch(master=frame_1)
|
||||
switch_1.pack(pady=y_padding, padx=10)
|
||||
switch_1.pack(pady=12, padx=10)
|
||||
|
||||
root_tk.mainloop()
|
||||
app.mainloop()
|
||||
|
@ -3,7 +3,7 @@ import tkinter
|
||||
|
||||
app = tkinter.Tk()
|
||||
app.geometry("400x350")
|
||||
app.title("Standard Tkinter Test")
|
||||
app.title("simple_example_standard_tkinter.py")
|
||||
|
||||
|
||||
def button_function():
|
||||
|
@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
|
||||
github_url = "https://github.com/TomSchimansky/CustomTkinter"
|
||||
|
||||
[tool.tbump.version]
|
||||
current = "4.0.0"
|
||||
current = "4.3.0"
|
||||
|
||||
# Example of a semver regexp.
|
||||
# Make sure this matches current_version before
|
||||
|
@ -1,8 +1,8 @@
|
||||
[metadata]
|
||||
name = customtkinter
|
||||
version = 4.0.0
|
||||
description = Create modern looking GUIs with tkinter and Python
|
||||
long_description = file: README.md
|
||||
version = 4.3.0
|
||||
description = Create modern looking GUIs with Python
|
||||
long_description = '# CustomTkinter UI-Library\nhttps://github.com/TomSchimansky/CustomTkinter/blob/master/documentation_images/Windows_dark.png\n\nMore Information: https://github.com/TomSchimansky/CustomTkinter'
|
||||
long_description_content_type = text/markdown
|
||||
url = https://github.com/TomSchimansky/CustomTkinter
|
||||
author = Tom Schimansky
|
||||
@ -14,10 +14,12 @@ classifiers =
|
||||
Programming Language :: Python :: 3 :: Only
|
||||
|
||||
[options]
|
||||
python_requires = >=3.7
|
||||
packages =
|
||||
customtkinter
|
||||
customtkinter.widgets
|
||||
customtkinter.windows
|
||||
install_requires =
|
||||
darkdetect
|
||||
typing_extensions; python_version<="3.7"
|
||||
include_package_data = True
|
||||
|
@ -1,9 +1,7 @@
|
||||
from tkinter.constants import CENTER, LEFT
|
||||
import tkinter
|
||||
import tkinter.messagebox
|
||||
from tkinter import filedialog as fd
|
||||
import customtkinter # <- import the CustomTkinter module
|
||||
import os
|
||||
import customtkinter
|
||||
|
||||
|
||||
class App(customtkinter.CTk):
|
||||
@ -32,7 +30,6 @@ class App(customtkinter.CTk):
|
||||
height=App.HEIGHT-40,
|
||||
corner_radius=5)
|
||||
self.frame_left.place(relx=0.38, rely=0.5, anchor=tkinter.E)
|
||||
print(self.frame_left.widget_scaling)
|
||||
|
||||
self.frame_right = customtkinter.CTkFrame(master=self,
|
||||
width=350,
|
||||
@ -65,4 +62,4 @@ class App(customtkinter.CTk):
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = App()
|
||||
app.start()
|
||||
app.start()
|
||||
|
@ -1,5 +1,4 @@
|
||||
import customtkinter
|
||||
import tkinter
|
||||
|
||||
customtkinter.set_default_color_theme("blue")
|
||||
customtkinter.set_appearance_mode("dark")
|
||||
|
@ -1,33 +0,0 @@
|
||||
import tkinter
|
||||
import customtkinter # <- import the CustomTkinter module
|
||||
|
||||
customtkinter.set_appearance_mode("System") # Other: "Dark", "Light"
|
||||
|
||||
root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
|
||||
root_tk.geometry("400x240")
|
||||
root_tk.title("CustomTkinter Test")
|
||||
|
||||
|
||||
def change_button_2_state():
|
||||
if button_2.state == tkinter.NORMAL:
|
||||
button_2.configure(state=tkinter.DISABLED)
|
||||
elif button_2.state == tkinter.DISABLED:
|
||||
button_2.configure(state=tkinter.NORMAL)
|
||||
|
||||
|
||||
def button_2_click():
|
||||
print("button_2 clicked")
|
||||
|
||||
|
||||
frame_1 = customtkinter.CTkFrame(master=root_tk, width=300, height=200, corner_radius=15)
|
||||
frame_1.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
|
||||
|
||||
button_1 = customtkinter.CTkButton(master=frame_1, text="Disable/Enable Button_2",
|
||||
corner_radius=10, command=change_button_2_state, width=200)
|
||||
button_1.place(relx=0.5, rely=0.3, anchor=tkinter.CENTER)
|
||||
|
||||
button_2 = customtkinter.CTkButton(master=frame_1, text="Button_2",
|
||||
corner_radius=10, command=button_2_click)
|
||||
button_2.place(relx=0.5, rely=0.7, anchor=tkinter.CENTER)
|
||||
|
||||
root_tk.mainloop()
|
36
test/manual_integration_tests/test_configure_dimensions.py
Normal file
36
test/manual_integration_tests/test_configure_dimensions.py
Normal file
@ -0,0 +1,36 @@
|
||||
import customtkinter
|
||||
import random
|
||||
|
||||
|
||||
app = customtkinter.CTk()
|
||||
app.geometry("400x400")
|
||||
|
||||
|
||||
def button_callback():
|
||||
button_1.configure(width=random.randint(30, 200), height=random.randint(30, 60))
|
||||
frame_1.configure(width=random.randint(30, 200), height=random.randint(30, 200))
|
||||
label_1.configure(width=random.randint(30, 200), height=random.randint(30, 40))
|
||||
entry_1.configure(width=random.randint(30, 200), height=random.randint(30, 40))
|
||||
progressbar_1.configure(width=random.randint(30, 200), height=random.randint(10, 16))
|
||||
slider_1.configure(width=random.randint(30, 200), height=random.randint(14, 20))
|
||||
|
||||
|
||||
button_1 = customtkinter.CTkButton(app, text="button_1", command=button_callback)
|
||||
button_1.pack(pady=10)
|
||||
|
||||
frame_1 = customtkinter.CTkFrame(app)
|
||||
frame_1.pack(pady=10)
|
||||
|
||||
label_1 = customtkinter.CTkLabel(app, fg_color="green")
|
||||
label_1.pack(pady=10)
|
||||
|
||||
entry_1 = customtkinter.CTkEntry(app, placeholder_text="placeholder")
|
||||
entry_1.pack(pady=10)
|
||||
|
||||
progressbar_1 = customtkinter.CTkProgressBar(app)
|
||||
progressbar_1.pack(pady=10)
|
||||
|
||||
slider_1 = customtkinter.CTkSlider(app)
|
||||
slider_1.pack(pady=10)
|
||||
|
||||
app.mainloop()
|
@ -1,4 +1,3 @@
|
||||
import tkinter
|
||||
import customtkinter
|
||||
|
||||
|
||||
@ -15,11 +14,9 @@ class ExampleApp(customtkinter.CTk):
|
||||
window = customtkinter.CTkToplevel(self)
|
||||
window.geometry("400x200")
|
||||
|
||||
print(window.master.winfo_class())
|
||||
|
||||
label = customtkinter.CTkLabel(window, text="CTkToplevel window")
|
||||
label.pack(side="top", fill="both", expand=True, padx=40, pady=40)
|
||||
|
||||
|
||||
app = ExampleApp()
|
||||
app.mainloop()
|
||||
app.mainloop()
|
||||
|
@ -1,22 +1,21 @@
|
||||
import tkinter
|
||||
import customtkinter
|
||||
|
||||
root_tk = customtkinter.CTk()
|
||||
root_tk.geometry("400x240")
|
||||
app = customtkinter.CTk()
|
||||
app.geometry("400x240")
|
||||
|
||||
|
||||
def button_function():
|
||||
|
||||
top = customtkinter.CTkToplevel(root_tk)
|
||||
top = customtkinter.CTkToplevel(app)
|
||||
|
||||
root_tk.after(1000, top.iconify) # hide toplevel
|
||||
root_tk.after(1500, top.deiconify) # show toplevel
|
||||
root_tk.after(2500, root_tk.iconify) # hide root_tk
|
||||
root_tk.after(3000, root_tk.deiconify) # show root_tk
|
||||
root_tk.after(4000, root_tk.destroy) # destroy everything
|
||||
app.after(1000, top.iconify) # hide toplevel
|
||||
app.after(1500, top.deiconify) # show toplevel
|
||||
app.after(2500, app.iconify) # hide app
|
||||
app.after(3000, app.deiconify) # show app
|
||||
app.after(4000, app.destroy) # destroy everything
|
||||
|
||||
|
||||
button = customtkinter.CTkButton(root_tk, command=button_function)
|
||||
button = customtkinter.CTkButton(app, command=button_function)
|
||||
button.pack(pady=20, padx=20)
|
||||
|
||||
root_tk.mainloop()
|
||||
app.mainloop()
|
||||
|
@ -1,53 +1,71 @@
|
||||
import customtkinter
|
||||
import tkinter
|
||||
|
||||
# customtkinter.set_appearance_mode("light")
|
||||
|
||||
root_tk = customtkinter.CTk()
|
||||
root_tk.geometry("600x500")
|
||||
|
||||
menu = tkinter.Menu(tearoff=0, bd=0, relief=tkinter.FLAT, activeforeground="red")
|
||||
menu.add_command(label="System")
|
||||
menu.add_command(label="Light")
|
||||
menu.add_command(label="Dark")
|
||||
import sys
|
||||
|
||||
|
||||
class CTkMenu(tkinter.Toplevel):
|
||||
def __init__(self, master, x, y, options):
|
||||
super().__init__(bg="black")
|
||||
super().overrideredirect(True)
|
||||
#self.wm_attributes("-transparentcolor", "black")
|
||||
super().geometry(f"120x{len(options) * (25 + 4) + 4}+{x}+{y}")
|
||||
super().lift()
|
||||
super().transient(master)
|
||||
self.resizable(False, False)
|
||||
super().focus_force()
|
||||
self.focus()
|
||||
super().__init__()
|
||||
|
||||
self.frame = customtkinter.CTkFrame(self, border_width=0, width=120, corner_radius=10, border_color="gray4", fg_color="#333740")
|
||||
self.frame.grid(row=0, column=0, sticky="nsew", rowspan=len(options) + 2, columnspan=1)
|
||||
self.overrideredirect(True)
|
||||
self.geometry(f"120x{len(options) * (25 + 3) + 3}+{x}+{y}")
|
||||
|
||||
self.frame.grid_rowconfigure(0, minsize=2)
|
||||
self.frame.grid_rowconfigure(len(options) + 1, minsize=2)
|
||||
if sys.platform.startswith("darwin"):
|
||||
self.overrideredirect(False)
|
||||
self.wm_attributes("-transparent", True) # turn off shadow
|
||||
self.config(bg='systemTransparent') # transparent bg
|
||||
self.frame = customtkinter.CTkFrame(self, border_width=0, width=120, corner_radius=6, border_color="gray4", fg_color="#333740")
|
||||
elif sys.platform.startswith("win"):
|
||||
self.configure(bg="#FFFFF1")
|
||||
self.wm_attributes("-transparent", "#FFFFF1")
|
||||
self.focus()
|
||||
self.frame = customtkinter.CTkFrame(self, border_width=0, width=120, corner_radius=6, border_color="gray4", fg_color="#333740",
|
||||
overwrite_preferred_drawing_method="circle_shapes")
|
||||
else:
|
||||
self.configure(bg="#FFFFF1")
|
||||
self.wm_attributes("-transparent", "#FFFFF1")
|
||||
self.frame = customtkinter.CTkFrame(self, border_width=0, width=120, corner_radius=6, border_color="gray4", fg_color="#333740",
|
||||
overwrite_preferred_drawing_method="circle_shapes")
|
||||
|
||||
self.frame.grid(row=0, column=0, sticky="nsew", rowspan=len(options) + 1, columnspan=1, ipadx=0, ipady=0)
|
||||
|
||||
self.frame.grid_rowconfigure(len(options) + 1, minsize=3)
|
||||
self.frame.grid_columnconfigure(0, weight=1)
|
||||
self.grid_columnconfigure(0, weight=1)
|
||||
|
||||
self.buttons = []
|
||||
for index, option in enumerate(options):
|
||||
button = customtkinter.CTkButton(self.frame, height=25, width=108, fg_color="#333740", text_color="gray74", hover_color="#272A2E", corner_radius=8)
|
||||
button = customtkinter.CTkButton(self.frame, text=option, height=25, width=108, fg_color="#333740", text_color="gray74",
|
||||
hover_color="gray28", corner_radius=4, command=self.button_click)
|
||||
button.text_label.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="w")
|
||||
button.grid(row=index + 1, column=0, padx=4, pady=2)
|
||||
button.grid(row=index, column=0, padx=(3, 3), pady=(3, 0), columnspan=1, rowspan=1, sticky="ew")
|
||||
self.buttons.append(button)
|
||||
|
||||
# master.bind("<Configure>", self.window_drag())
|
||||
self.bind("<FocusOut>", self.focus_loss_event)
|
||||
self.frame.canvas.bind("<Button-1>", self.focus_loss_event)
|
||||
|
||||
def focus_loss_event(self, event):
|
||||
print("focus loss")
|
||||
self.destroy()
|
||||
# self.update()
|
||||
|
||||
def button_click(self):
|
||||
print("button press")
|
||||
self.destroy()
|
||||
# self.update()
|
||||
|
||||
|
||||
app = customtkinter.CTk()
|
||||
app.geometry("600x500")
|
||||
|
||||
|
||||
def open_menu():
|
||||
menu = CTkMenu(root_tk, button.winfo_rootx(), button.winfo_rooty() + button.winfo_height() + 4, ["Option 1", "Option 2", "Point 3"])
|
||||
menu = CTkMenu(app, button.winfo_rootx(), button.winfo_rooty() + button.winfo_height() + 4, ["Option 1", "Option 2", "Point 3"])
|
||||
|
||||
button = customtkinter.CTkButton(command=open_menu, height=50)
|
||||
button = customtkinter.CTkButton(command=open_menu, height=30, corner_radius=6)
|
||||
button.pack(pady=20)
|
||||
|
||||
root_tk.mainloop()
|
||||
button_2 = customtkinter.CTkButton(command=open_menu, height=30, corner_radius=6)
|
||||
button_2.pack(pady=60)
|
||||
|
||||
app.mainloop()
|
||||
|
31
test/manual_integration_tests/test_optionmenu_combobox.py
Normal file
31
test/manual_integration_tests/test_optionmenu_combobox.py
Normal file
@ -0,0 +1,31 @@
|
||||
import tkinter
|
||||
import customtkinter
|
||||
|
||||
app = customtkinter.CTk()
|
||||
app.title('Test OptionMenu ComboBox.py')
|
||||
app.geometry('400x300')
|
||||
|
||||
|
||||
def select_callback(choice):
|
||||
choice = variable.get()
|
||||
print("display_selected", choice)
|
||||
|
||||
|
||||
countries = ['Bahamas', 'Canada', 'Cuba', 'United States']
|
||||
|
||||
variable = tkinter.StringVar()
|
||||
variable.set("test")
|
||||
|
||||
# optionmenu_tk = tkinter.OptionMenu(app, variable, *countries, command=select_callback)
|
||||
# optionmenu_tk.pack(pady=10, padx=10)
|
||||
|
||||
optionmenu_1 = customtkinter.CTkOptionMenu(app, variable=variable, values=countries, command=select_callback)
|
||||
optionmenu_1.pack(pady=20, padx=10)
|
||||
|
||||
# combobox_tk = ttk.Combobox(app, values=countries)
|
||||
# combobox_tk.pack(pady=10, padx=10)
|
||||
|
||||
combobox_1 = customtkinter.CTkComboBox(app, variable=variable, values=countries, command=select_callback)
|
||||
combobox_1.pack(pady=20, padx=10)
|
||||
|
||||
app.mainloop()
|
@ -6,17 +6,17 @@ customtkinter.ScalingTracker.set_window_scaling(0.5)
|
||||
customtkinter.set_appearance_mode("dark") # Modes: "System" (standard), "Dark", "Light"
|
||||
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
|
||||
|
||||
root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
|
||||
root_tk.geometry("400x600")
|
||||
root_tk.title("CustomTkinter manual scaling test")
|
||||
app = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
|
||||
app.geometry("400x600")
|
||||
app.title("CustomTkinter manual scaling test")
|
||||
|
||||
#root_tk.minsize(200, 200)
|
||||
#root_tk.maxsize(520, 520)
|
||||
#root_tk.resizable(True, False)
|
||||
#app.minsize(200, 200)
|
||||
#app.maxsize(520, 520)
|
||||
#app.resizable(True, False)
|
||||
|
||||
|
||||
def button_function():
|
||||
root_tk.geometry(f"{200}x{200}")
|
||||
app.geometry(f"{200}x{200}")
|
||||
print("Button click", label_1.text_label.cget("text"))
|
||||
|
||||
|
||||
@ -29,7 +29,7 @@ def slider_function(value):
|
||||
|
||||
y_padding = 13
|
||||
|
||||
frame_1 = customtkinter.CTkFrame(master=root_tk, height=550, width=300)
|
||||
frame_1 = customtkinter.CTkFrame(master=app, height=550, width=300)
|
||||
frame_1.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
|
||||
label_1 = customtkinter.CTkLabel(master=frame_1, justify=tkinter.LEFT)
|
||||
label_1.place(relx=0.5, y=50, anchor=tkinter.CENTER)
|
||||
@ -53,4 +53,4 @@ s_var = tkinter.StringVar(value="on")
|
||||
switch_1 = customtkinter.CTkSwitch(master=frame_1)
|
||||
switch_1.place(relx=0.5, y=450, anchor=tkinter.CENTER)
|
||||
|
||||
root_tk.mainloop()
|
||||
app.mainloop()
|
||||
|
@ -6,32 +6,33 @@ customtkinter.ScalingTracker.set_window_scaling(0.5)
|
||||
customtkinter.set_appearance_mode("dark") # Modes: "System" (standard), "Dark", "Light"
|
||||
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
|
||||
|
||||
root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
|
||||
root_tk.geometry("400x480")
|
||||
root_tk.title("CustomTkinter manual scaling test")
|
||||
app = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
|
||||
app.geometry("400x480")
|
||||
app.title("CustomTkinter manual scaling test")
|
||||
|
||||
top_tk = customtkinter.CTkToplevel(root_tk)
|
||||
top_tk = customtkinter.CTkToplevel(app)
|
||||
top_tk.geometry("500x500")
|
||||
|
||||
#root_tk.minsize(200, 200)
|
||||
#root_tk.maxsize(520, 520)
|
||||
#root_tk.resizable(True, False)
|
||||
#app.minsize(200, 200)
|
||||
#app.maxsize(520, 520)
|
||||
#app.resizable(True, False)
|
||||
|
||||
|
||||
def button_function():
|
||||
root_tk.geometry(f"{200}x{200}")
|
||||
app.geometry(f"{200}x{200}")
|
||||
print("Button click", label_1.text_label.cget("text"))
|
||||
|
||||
|
||||
def slider_function(value):
|
||||
customtkinter.set_widget_scaling(value * 2)
|
||||
customtkinter.ScalingTracker.set_window_scaling(value * 2)
|
||||
customtkinter.set_spacing_scaling(value * 2)
|
||||
customtkinter.set_window_scaling(value * 2)
|
||||
progressbar_1.set(value)
|
||||
|
||||
|
||||
y_padding = 13
|
||||
|
||||
frame_1 = customtkinter.CTkFrame(master=root_tk)
|
||||
frame_1 = customtkinter.CTkFrame(master=app)
|
||||
frame_1.pack(pady=20, padx=60, fill="both", expand=True)
|
||||
label_1 = customtkinter.CTkLabel(master=frame_1, justify=tkinter.LEFT)
|
||||
label_1.pack(pady=y_padding, padx=10)
|
||||
@ -77,4 +78,4 @@ radiobutton_2.pack(pady=y_padding, padx=10)
|
||||
switch_1 = customtkinter.CTkSwitch(master=top_tk)
|
||||
switch_1.pack(pady=y_padding, padx=10)
|
||||
|
||||
root_tk.mainloop()
|
||||
app.mainloop()
|
||||
|
@ -1,16 +0,0 @@
|
||||
import tkinter
|
||||
|
||||
app = tkinter.Tk()
|
||||
app.geometry("600x500")
|
||||
|
||||
|
||||
canvas = tkinter.Canvas(master=app, highlightthickness=0, bg="gray30")
|
||||
canvas.pack(expand=True, fill="both")
|
||||
|
||||
text_1 = canvas.create_text(100, 100, text="⬤", font=('Helvetica', -4, 'bold'))
|
||||
text_2 = canvas.create_text(100, 200, text="⬤", font=('Helvetica', -8, 'bold'))
|
||||
text_3 = canvas.create_text(100, 300, text="⬤", font=('Helvetica', -12, 'bold'))
|
||||
text_4 = canvas.create_text(100, 400, text="⬤", font=('Helvetica', -20, 'bold'))
|
||||
text_5 = canvas.create_text(400, 400, text="⬤", font=('Helvetica', -100, 'bold'))
|
||||
|
||||
app.mainloop()
|
@ -1,5 +1,4 @@
|
||||
import customtkinter
|
||||
import tkinter
|
||||
|
||||
customtkinter.set_appearance_mode("dark")
|
||||
customtkinter.set_default_color_theme("blue")
|
||||
@ -22,8 +21,8 @@ def button_click_event():
|
||||
|
||||
|
||||
button = customtkinter.CTkButton(app, text="Open Dialog", command=button_click_event)
|
||||
button.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
|
||||
button.place(relx=0.5, rely=0.5, anchor=customtkinter.CENTER)
|
||||
c1 = customtkinter.CTkCheckBox(app, text="dark mode", command=change_mode)
|
||||
c1.place(relx=0.5, rely=0.8, anchor=tkinter.CENTER)
|
||||
c1.place(relx=0.5, rely=0.8, anchor=customtkinter.CENTER)
|
||||
|
||||
app.mainloop()
|
||||
app.mainloop()
|
||||
|
@ -1,63 +1,63 @@
|
||||
import tkinter
|
||||
import customtkinter # <- import the CustomTkinter module
|
||||
import customtkinter
|
||||
|
||||
TEST_CONFIGURE = True
|
||||
TEST_REMOVING = False
|
||||
|
||||
root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
|
||||
root_tk.geometry("400x600")
|
||||
root_tk.title("Tkinter Variable Test")
|
||||
app = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
|
||||
app.geometry("400x800")
|
||||
app.title("Tkinter Variable Test")
|
||||
|
||||
txt_var = tkinter.StringVar(value="")
|
||||
entry_1 = customtkinter.CTkEntry(root_tk, width=200, textvariable=txt_var)
|
||||
entry_1 = customtkinter.CTkEntry(app, width=200, textvariable=txt_var)
|
||||
entry_1.pack(pady=15)
|
||||
txt_var.set("new text wjkfjdshkjfb")
|
||||
txt_var.set("new text test")
|
||||
if TEST_CONFIGURE: entry_1.configure(textvariable=txt_var)
|
||||
if TEST_REMOVING: entry_1.configure(textvariable="")
|
||||
|
||||
label_1 = customtkinter.CTkLabel(root_tk, width=200, textvariable=txt_var)
|
||||
label_1 = customtkinter.CTkLabel(app, width=200, textvariable=txt_var)
|
||||
label_1.pack(pady=15)
|
||||
if TEST_CONFIGURE: label_1.configure(textvariable=txt_var)
|
||||
if TEST_REMOVING: label_1.configure(textvariable="")
|
||||
|
||||
button_1 = customtkinter.CTkButton(root_tk, width=200, textvariable=txt_var)
|
||||
button_1 = customtkinter.CTkButton(app, width=200, textvariable=txt_var)
|
||||
button_1.pack(pady=15)
|
||||
int_var = tkinter.IntVar(value=10)
|
||||
if TEST_CONFIGURE: button_1.configure(textvariable=int_var)
|
||||
if TEST_REMOVING: button_1.configure(textvariable="")
|
||||
|
||||
slider_1 = customtkinter.CTkSlider(root_tk, width=200, from_=0, to=3, variable=int_var)
|
||||
slider_1 = customtkinter.CTkSlider(app, width=200, from_=0, to=3, variable=int_var)
|
||||
slider_1.pack(pady=15)
|
||||
if TEST_CONFIGURE: slider_1.configure(variable=int_var)
|
||||
if TEST_REMOVING: slider_1.configure(variable="")
|
||||
int_var.set(2)
|
||||
|
||||
slider_2 = customtkinter.CTkSlider(root_tk, width=200, from_=0, to=3, variable=int_var)
|
||||
slider_2 = customtkinter.CTkSlider(app, width=200, from_=0, to=3, variable=int_var)
|
||||
slider_2.pack(pady=15)
|
||||
if TEST_CONFIGURE: slider_2.configure(variable=int_var)
|
||||
if TEST_REMOVING: slider_2.configure(variable="")
|
||||
|
||||
label_2 = customtkinter.CTkLabel(root_tk, width=200, textvariable=int_var)
|
||||
label_2 = customtkinter.CTkLabel(app, width=200, textvariable=int_var)
|
||||
label_2.pack(pady=15)
|
||||
|
||||
progress_1 = customtkinter.CTkProgressBar(root_tk, width=200, variable=int_var)
|
||||
progress_1 = customtkinter.CTkProgressBar(app, width=200, variable=int_var)
|
||||
progress_1.pack(pady=15)
|
||||
if TEST_CONFIGURE: progress_1.configure(variable=int_var)
|
||||
if TEST_REMOVING: progress_1.configure(variable="")
|
||||
|
||||
check_var = tkinter.StringVar(value="on")
|
||||
check_1 = customtkinter.CTkCheckBox(root_tk, text="check 1", variable=check_var, onvalue="on", offvalue="off")
|
||||
check_1 = customtkinter.CTkCheckBox(app, text="check 1", variable=check_var, onvalue="on", offvalue="off")
|
||||
check_1.pack(pady=15)
|
||||
if TEST_CONFIGURE: check_1.configure(variable=check_var)
|
||||
if TEST_REMOVING: check_1.configure(variable="")
|
||||
print("check_1", check_1.get())
|
||||
|
||||
check_2 = customtkinter.CTkCheckBox(root_tk, text="check 2", variable=check_var, onvalue="on", offvalue="off")
|
||||
check_2 = customtkinter.CTkCheckBox(app, text="check 2", variable=check_var, onvalue="on", offvalue="off")
|
||||
check_2.pack(pady=15)
|
||||
if TEST_CONFIGURE: check_2.configure(variable=check_var)
|
||||
if TEST_REMOVING: check_2.configure(variable="")
|
||||
|
||||
label_3 = customtkinter.CTkLabel(root_tk, width=200, textvariable=check_var)
|
||||
label_3 = customtkinter.CTkLabel(app, width=200, textvariable=check_var)
|
||||
label_3.pack(pady=15)
|
||||
label_3.configure(textvariable=check_var)
|
||||
|
||||
@ -65,10 +65,16 @@ def switch_event():
|
||||
print("switch event")
|
||||
|
||||
s_var = tkinter.StringVar(value="on")
|
||||
switch_1 = customtkinter.CTkSwitch(master=root_tk, variable=s_var, textvariable=s_var, onvalue="on", offvalue="off", command=switch_event)
|
||||
switch_1 = customtkinter.CTkSwitch(master=app, variable=s_var, textvariable=s_var, onvalue="on", offvalue="off", command=switch_event)
|
||||
switch_1.pack(pady=20, padx=10)
|
||||
switch_1 = customtkinter.CTkSwitch(master=root_tk, variable=s_var, textvariable=s_var, onvalue="on", offvalue="off")
|
||||
switch_1 = customtkinter.CTkSwitch(master=app, variable=s_var, textvariable=s_var, onvalue="on", offvalue="off")
|
||||
switch_1.pack(pady=20, padx=10)
|
||||
#switch_1.toggle()
|
||||
|
||||
root_tk.mainloop()
|
||||
optionmenu_var = tkinter.StringVar(value="test")
|
||||
optionmenu_1 = customtkinter.CTkOptionMenu(master=app, variable=optionmenu_var, values=["Option 1", "Option 2", "Option 3"])
|
||||
optionmenu_1.pack(pady=20, padx=10)
|
||||
combobox_1 = customtkinter.CTkComboBox(master=app, values=["Option 1", "Option 2", "Option 3"])
|
||||
combobox_1.pack(pady=20, padx=10)
|
||||
combobox_1.configure(variable=optionmenu_var)
|
||||
|
||||
app.mainloop()
|
||||
|
@ -4,28 +4,28 @@ import customtkinter
|
||||
|
||||
customtkinter.set_appearance_mode("light")
|
||||
|
||||
root_tk = customtkinter.CTk()
|
||||
root_tk.geometry("1400x480")
|
||||
root_tk.title("CustomTkinter TTk Compatibility Test")
|
||||
app = customtkinter.CTk()
|
||||
app.geometry("1400x480")
|
||||
app.title("CustomTkinter TTk Compatibility Test")
|
||||
|
||||
root_tk.grid_rowconfigure(0, weight=1)
|
||||
root_tk.grid_columnconfigure((0, 1, 2, 3, 5, 6), weight=1)
|
||||
app.grid_rowconfigure(0, weight=1)
|
||||
app.grid_columnconfigure((0, 1, 2, 3, 5, 6), weight=1)
|
||||
|
||||
|
||||
button_0 = customtkinter.CTkButton(root_tk)
|
||||
button_0 = customtkinter.CTkButton(app)
|
||||
button_0.grid(padx=20, pady=20, row=0, column=0)
|
||||
|
||||
frame_1 = tkinter.Frame(master=root_tk)
|
||||
frame_1 = tkinter.Frame(master=app)
|
||||
frame_1.grid(padx=20, pady=20, row=0, column=1, sticky="nsew")
|
||||
button_1 = customtkinter.CTkButton(frame_1, text="tkinter.Frame")
|
||||
button_1.pack(pady=20, padx=20)
|
||||
|
||||
frame_2 = tkinter.LabelFrame(master=root_tk, text="Tkinter LabelFrame")
|
||||
frame_2 = tkinter.LabelFrame(master=app, text="Tkinter LabelFrame")
|
||||
frame_2.grid(padx=20, pady=20, row=0, column=2, sticky="nsew")
|
||||
button_2 = customtkinter.CTkButton(frame_2, text="tkinter.LabelFrame")
|
||||
button_2.pack(pady=20, padx=20)
|
||||
|
||||
frame_3 = customtkinter.CTkFrame(master=root_tk)
|
||||
frame_3 = customtkinter.CTkFrame(master=app)
|
||||
frame_3.grid(padx=20, pady=20, row=0, column=3, sticky="nsew")
|
||||
label_3 = customtkinter.CTkLabel(master=frame_3, text="CTkFrame Label", fg_color=("gray95", "gray15"))
|
||||
label_3.grid(row=0, column=0, columnspan=1, padx=5, pady=5, sticky="ew")
|
||||
@ -34,17 +34,17 @@ button_3.grid(row=1, column=0, padx=20)
|
||||
frame_3.grid_rowconfigure(1, weight=1)
|
||||
frame_3.grid_columnconfigure((0, ), weight=1)
|
||||
|
||||
frame_4 = ttk.Frame(master=root_tk)
|
||||
frame_4 = ttk.Frame(master=app)
|
||||
frame_4.grid(padx=20, pady=20, row=0, column=4, sticky="nsew")
|
||||
button_4 = customtkinter.CTkButton(frame_4, text="ttk.Frame")
|
||||
button_4.pack(pady=20, padx=20)
|
||||
|
||||
frame_5 = ttk.LabelFrame(master=root_tk, text="TTk LabelFrame")
|
||||
frame_5 = ttk.LabelFrame(master=app, text="TTk LabelFrame")
|
||||
frame_5.grid(padx=20, pady=20, row=0, column=5, sticky="nsew")
|
||||
button_5 = customtkinter.CTkButton(frame_5)
|
||||
button_5.pack(pady=20, padx=20)
|
||||
|
||||
frame_6 = ttk.Notebook(master=root_tk)
|
||||
frame_6 = ttk.Notebook(master=app)
|
||||
frame_6.grid(padx=20, pady=20, row=0, column=6, sticky="nsew")
|
||||
button_6 = customtkinter.CTkButton(frame_6, text="ttk.Notebook")
|
||||
button_6.pack(pady=20, padx=20)
|
||||
@ -52,4 +52,4 @@ button_6.pack(pady=20, padx=20)
|
||||
ttk_style = ttk.Style()
|
||||
ttk_style.configure(frame_3.winfo_class(), background='red')
|
||||
|
||||
root_tk.mainloop()
|
||||
app.mainloop()
|
||||
|
@ -1,25 +1,25 @@
|
||||
import customtkinter
|
||||
|
||||
root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
|
||||
root_tk.geometry("400x650")
|
||||
root_tk.title("test_vertical_widgets")
|
||||
app = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
|
||||
app.geometry("400x650")
|
||||
app.title("test_vertical_widgets")
|
||||
|
||||
root_tk.grid_columnconfigure(0, weight=1)
|
||||
root_tk.grid_rowconfigure((0, 1, 2, 3), weight=1)
|
||||
app.grid_columnconfigure(0, weight=1)
|
||||
app.grid_rowconfigure((0, 1, 2, 3), weight=1)
|
||||
|
||||
progressbar_1 = customtkinter.CTkProgressBar(root_tk, orient="horizontal")
|
||||
progressbar_1 = customtkinter.CTkProgressBar(app, orient="horizontal")
|
||||
progressbar_1.grid(row=0, column=0, pady=20, padx=20)
|
||||
|
||||
progressbar_2 = customtkinter.CTkProgressBar(root_tk, orient="vertical")
|
||||
progressbar_2 = customtkinter.CTkProgressBar(app, orient="vertical")
|
||||
progressbar_2.grid(row=1, column=0, pady=20, padx=20)
|
||||
|
||||
slider_1 = customtkinter.CTkSlider(root_tk, orient="horizontal", command=progressbar_1.set,
|
||||
slider_1 = customtkinter.CTkSlider(app, orient="horizontal", command=progressbar_1.set,
|
||||
button_corner_radius=3, button_length=20)
|
||||
slider_1.grid(row=2, column=0, pady=20, padx=20)
|
||||
|
||||
slider_2 = customtkinter.CTkSlider(root_tk, orient="vertical", command=progressbar_2.set,
|
||||
slider_2 = customtkinter.CTkSlider(app, orient="vertical", command=progressbar_2.set,
|
||||
button_corner_radius=3, button_length=20)
|
||||
slider_2.grid(row=3, column=0, pady=20, padx=20)
|
||||
|
||||
|
||||
root_tk.mainloop()
|
||||
app.mainloop()
|
||||
|
57
test/manual_integration_tests/test_widget_states.py
Normal file
57
test/manual_integration_tests/test_widget_states.py
Normal file
@ -0,0 +1,57 @@
|
||||
import tkinter
|
||||
import customtkinter
|
||||
|
||||
|
||||
app = customtkinter.CTk()
|
||||
app.geometry("400x800")
|
||||
app.title("CustomTkinter Test")
|
||||
|
||||
|
||||
def change_state(widget):
|
||||
if widget.state == tkinter.NORMAL:
|
||||
widget.configure(state=tkinter.DISABLED)
|
||||
elif widget.state == tkinter.DISABLED:
|
||||
widget.configure(state=tkinter.NORMAL)
|
||||
|
||||
|
||||
def widget_click():
|
||||
print("widget clicked")
|
||||
|
||||
|
||||
button_1 = customtkinter.CTkButton(master=app, text="button_1", command=widget_click)
|
||||
button_1.pack(padx=20, pady=(20, 10))
|
||||
button_2 = customtkinter.CTkButton(master=app, text="Disable/Enable button_1", command=lambda: change_state(button_1))
|
||||
button_2.pack(padx=20, pady=(10, 20))
|
||||
|
||||
switch_1 = customtkinter.CTkSwitch(master=app, text="switch_1", command=widget_click)
|
||||
switch_1.pack(padx=20, pady=(20, 10))
|
||||
button_2 = customtkinter.CTkButton(master=app, text="Disable/Enable switch_1", command=lambda: change_state(switch_1))
|
||||
button_2.pack(padx=20, pady=(10, 20))
|
||||
|
||||
entry_1 = customtkinter.CTkEntry(master=app, placeholder_text="entry_1")
|
||||
entry_1.pack(padx=20, pady=(20, 10))
|
||||
button_3 = customtkinter.CTkButton(master=app, text="Disable/Enable entry_1", command=lambda: change_state(entry_1))
|
||||
button_3.pack(padx=20, pady=(10, 20))
|
||||
|
||||
checkbox_1 = customtkinter.CTkCheckBox(master=app, text="checkbox_1")
|
||||
checkbox_1.pack(padx=20, pady=(20, 10))
|
||||
button_4 = customtkinter.CTkButton(master=app, text="Disable/Enable checkbox_1", command=lambda: change_state(checkbox_1))
|
||||
button_4.pack(padx=20, pady=(10, 20))
|
||||
|
||||
radiobutton_1 = customtkinter.CTkRadioButton(master=app, text="radiobutton_1")
|
||||
radiobutton_1.pack(padx=20, pady=(20, 10))
|
||||
button_5 = customtkinter.CTkButton(master=app, text="Disable/Enable radiobutton_1", command=lambda: change_state(radiobutton_1))
|
||||
button_5.pack(padx=20, pady=(10, 20))
|
||||
|
||||
optionmenu_1 = customtkinter.CTkOptionMenu(app, values=["test 1", "test 2"])
|
||||
optionmenu_1.pack(pady=10, padx=10)
|
||||
button_6 = customtkinter.CTkButton(master=app, text="Disable/Enable optionmenu_1", command=lambda: change_state(optionmenu_1))
|
||||
button_6.pack(padx=20, pady=(10, 20))
|
||||
|
||||
combobox_1 = customtkinter.CTkComboBox(app, values=["test 1", "test 2"])
|
||||
combobox_1.pack(pady=10, padx=10)
|
||||
button_7 = customtkinter.CTkButton(master=app, text="Disable/Enable combobox_1", command=lambda: change_state(combobox_1))
|
||||
button_7.pack(padx=20, pady=(10, 20))
|
||||
|
||||
|
||||
app.mainloop()
|
@ -62,20 +62,15 @@ class TestCTk():
|
||||
|
||||
customtkinter.ScalingTracker.set_window_scaling(1.5)
|
||||
self.root_ctk.geometry("300x400")
|
||||
self.root_ctk.update()
|
||||
assert self.root_ctk.current_width == 300 and self.root_ctk.current_height == 400
|
||||
assert round(self.root_ctk.winfo_width()) == 450 and round(self.root_ctk.winfo_height()) == 600
|
||||
assert self.root_ctk.window_scaling == 1.5 * customtkinter.ScalingTracker.get_window_dpi_scaling(self.root_ctk)
|
||||
|
||||
self.root_ctk.maxsize(400, 500)
|
||||
self.root_ctk.geometry("500x500")
|
||||
self.root_ctk.update()
|
||||
assert self.root_ctk.current_width == 400 and self.root_ctk.current_height == 500
|
||||
assert round(self.root_ctk.winfo_width()) == 600 and round(self.root_ctk.winfo_height()) == 750
|
||||
|
||||
customtkinter.ScalingTracker.set_window_scaling(1)
|
||||
self.root_ctk.update()
|
||||
assert self.root_ctk.current_width == 400 and self.root_ctk.current_height == 500
|
||||
assert round(self.root_ctk.winfo_width()) == 400 and round(self.root_ctk.winfo_height()) == 500
|
||||
print("successful")
|
||||
|
||||
def test_configure(self):
|
||||
|
@ -64,20 +64,15 @@ class TestCTkToplevel():
|
||||
|
||||
customtkinter.ScalingTracker.set_window_scaling(1.5)
|
||||
self.ctk_toplevel.geometry("300x400")
|
||||
self.ctk_toplevel.update()
|
||||
assert self.ctk_toplevel.current_width == 300 and self.ctk_toplevel.current_height == 400
|
||||
assert round(self.ctk_toplevel.winfo_width()) == 450 and round(self.ctk_toplevel.winfo_height()) == 600
|
||||
assert self.root_ctk.window_scaling == 1.5 * customtkinter.ScalingTracker.get_window_dpi_scaling(self.root_ctk)
|
||||
|
||||
self.ctk_toplevel.maxsize(400, 500)
|
||||
self.ctk_toplevel.geometry("500x500")
|
||||
self.ctk_toplevel.update()
|
||||
assert self.ctk_toplevel.current_width == 400 and self.ctk_toplevel.current_height == 500
|
||||
assert round(self.ctk_toplevel.winfo_width()) == 600 and round(self.ctk_toplevel.winfo_height()) == 750
|
||||
|
||||
customtkinter.ScalingTracker.set_window_scaling(1)
|
||||
self.ctk_toplevel.update()
|
||||
assert self.ctk_toplevel.current_width == 400 and self.ctk_toplevel.current_height == 500
|
||||
assert round(self.ctk_toplevel.winfo_width()) == 400 and round(self.ctk_toplevel.winfo_height()) == 500
|
||||
print("successful")
|
||||
|
||||
def test_configure(self):
|
||||
|
Reference in New Issue
Block a user