ctk_label.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import tkinter
  2. from typing import Union, Tuple, Callable, Optional, Any
  3. from .core_rendering import CTkCanvas
  4. from .theme import ThemeManager
  5. from .core_rendering import DrawEngine
  6. from .core_widget_classes import CTkBaseClass
  7. from .font import CTkFont
  8. from .image import CTkImage
  9. from .utility import pop_from_dict_by_set, check_kwargs_empty
  10. class CTkLabel(CTkBaseClass):
  11. """
  12. Label with rounded corners. Default is fg_color=None (transparent fg_color).
  13. For detailed information check out the documentation.
  14. state argument will probably be removed because it has no effect
  15. """
  16. # attributes that are passed to and managed by the tkinter entry only:
  17. _valid_tk_label_attributes = {"cursor", "justify", "padx", "pady",
  18. "textvariable", "state", "takefocus", "underline"}
  19. def __init__(self,
  20. master: Any,
  21. width: int = 0,
  22. height: int = 28,
  23. corner_radius: Optional[int] = None,
  24. bg_color: Union[str, Tuple[str, str]] = "transparent",
  25. fg_color: Optional[Union[str, Tuple[str, str]]] = None,
  26. text_color: Optional[Union[str, Tuple[str, str]]] = None,
  27. text_color_disabled: Optional[Union[str, Tuple[str, str]]] = None,
  28. text: str = "CTkLabel",
  29. font: Optional[Union[tuple, CTkFont]] = None,
  30. image: Union[CTkImage, None] = None,
  31. compound: str = "center",
  32. anchor: str = "center", # label anchor: center, n, e, s, w
  33. wraplength: int = 0,
  34. **kwargs):
  35. # transfer basic functionality (_bg_color, size, __appearance_mode, scaling) to CTkBaseClass
  36. super().__init__(master=master, bg_color=bg_color, width=width, height=height)
  37. # color
  38. self._fg_color = ThemeManager.theme["CTkLabel"]["fg_color"] if fg_color is None else self._check_color_type(fg_color, transparency=True)
  39. self._text_color = ThemeManager.theme["CTkLabel"]["text_color"] if text_color is None else self._check_color_type(text_color)
  40. if text_color_disabled is None:
  41. if "text_color_disabled" in ThemeManager.theme["CTkLabel"]:
  42. self._text_color_disabled = ThemeManager.theme["CTkLabel"]["text_color"]
  43. else:
  44. self._text_color_disabled = self._text_color
  45. else:
  46. self._text_color_disabled = self._check_color_type(text_color_disabled)
  47. # shape
  48. self._corner_radius = ThemeManager.theme["CTkLabel"]["corner_radius"] if corner_radius is None else corner_radius
  49. # text
  50. self._anchor = anchor
  51. self._text = text
  52. self._wraplength = wraplength
  53. # image
  54. self._image = self._check_image_type(image)
  55. self._compound = compound
  56. if isinstance(self._image, CTkImage):
  57. self._image.add_configure_callback(self._update_image)
  58. # font
  59. self._font = CTkFont() if font is None else self._check_font_type(font)
  60. if isinstance(self._font, CTkFont):
  61. self._font.add_size_configure_callback(self._update_font)
  62. # configure grid system (1x1)
  63. self.grid_rowconfigure(0, weight=1)
  64. self.grid_columnconfigure(0, weight=1)
  65. self._canvas = CTkCanvas(master=self,
  66. highlightthickness=0,
  67. width=self._apply_widget_scaling(self._desired_width),
  68. height=self._apply_widget_scaling(self._desired_height))
  69. self._canvas.grid(row=0, column=0, sticky="nswe")
  70. self._draw_engine = DrawEngine(self._canvas)
  71. self._label = tkinter.Label(master=self,
  72. highlightthickness=0,
  73. padx=0,
  74. pady=0,
  75. borderwidth=0,
  76. anchor=self._anchor,
  77. compound=self._compound,
  78. wraplength=self._apply_widget_scaling(self._wraplength),
  79. text=self._text,
  80. font=self._apply_font_scaling(self._font))
  81. self._label.configure(**pop_from_dict_by_set(kwargs, self._valid_tk_label_attributes))
  82. check_kwargs_empty(kwargs, raise_error=True)
  83. self._create_grid()
  84. self._update_image()
  85. self._draw()
  86. def _set_scaling(self, *args, **kwargs):
  87. super()._set_scaling(*args, **kwargs)
  88. self._canvas.configure(width=self._apply_widget_scaling(self._desired_width), height=self._apply_widget_scaling(self._desired_height))
  89. self._label.configure(font=self._apply_font_scaling(self._font))
  90. self._label.configure(wraplength=self._apply_widget_scaling(self._wraplength))
  91. self._create_grid()
  92. self._update_image()
  93. self._draw(no_color_updates=True)
  94. def _set_appearance_mode(self, mode_string):
  95. super()._set_appearance_mode(mode_string)
  96. self._update_image()
  97. def _set_dimensions(self, width=None, height=None):
  98. super()._set_dimensions(width, height)
  99. self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
  100. height=self._apply_widget_scaling(self._desired_height))
  101. self._create_grid()
  102. self._draw()
  103. def _update_font(self):
  104. """ pass font to tkinter widgets with applied font scaling and update grid with workaround """
  105. self._label.configure(font=self._apply_font_scaling(self._font))
  106. # Workaround to force grid to be resized when text changes size.
  107. # Otherwise grid will lag and only resizes if other mouse action occurs.
  108. self._canvas.grid_forget()
  109. self._canvas.grid(row=0, column=0, sticky="nswe")
  110. def _update_image(self):
  111. if isinstance(self._image, CTkImage):
  112. self._label.configure(image=self._image.create_scaled_photo_image(self._get_widget_scaling(),
  113. self._get_appearance_mode()))
  114. elif self._image is not None:
  115. self._label.configure(image=self._image)
  116. def destroy(self):
  117. if isinstance(self._font, CTkFont):
  118. self._font.remove_size_configure_callback(self._update_font)
  119. super().destroy()
  120. def _create_grid(self):
  121. """ configure grid system (1x1) """
  122. text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
  123. self._label.grid(row=0, column=0, sticky=text_label_grid_sticky,
  124. padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height / 2))))
  125. def _draw(self, no_color_updates=False):
  126. super()._draw(no_color_updates)
  127. requires_recoloring = self._draw_engine.draw_rounded_rect_with_border(self._apply_widget_scaling(self._current_width),
  128. self._apply_widget_scaling(self._current_height),
  129. self._apply_widget_scaling(self._corner_radius),
  130. 0)
  131. if no_color_updates is False or requires_recoloring:
  132. if self._apply_appearance_mode(self._fg_color) == "transparent":
  133. self._canvas.itemconfig("inner_parts",
  134. fill=self._apply_appearance_mode(self._bg_color),
  135. outline=self._apply_appearance_mode(self._bg_color))
  136. self._label.configure(fg=self._apply_appearance_mode(self._text_color),
  137. disabledforeground=self._apply_appearance_mode(self._text_color_disabled),
  138. bg=self._apply_appearance_mode(self._bg_color))
  139. else:
  140. self._canvas.itemconfig("inner_parts",
  141. fill=self._apply_appearance_mode(self._fg_color),
  142. outline=self._apply_appearance_mode(self._fg_color))
  143. self._label.configure(fg=self._apply_appearance_mode(self._text_color),
  144. disabledforeground=self._apply_appearance_mode(self._text_color_disabled),
  145. bg=self._apply_appearance_mode(self._fg_color))
  146. self._canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
  147. def configure(self, require_redraw=False, **kwargs):
  148. if "corner_radius" in kwargs:
  149. self._corner_radius = kwargs.pop("corner_radius")
  150. self._create_grid()
  151. require_redraw = True
  152. if "fg_color" in kwargs:
  153. self._fg_color = self._check_color_type(kwargs.pop("fg_color"), transparency=True)
  154. require_redraw = True
  155. if "text_color" in kwargs:
  156. self._text_color = self._check_color_type(kwargs.pop("text_color"))
  157. require_redraw = True
  158. if "text_color_disabled" in kwargs:
  159. self._text_color_disabled = self._check_color_type(kwargs.pop("text_color_disabled"))
  160. require_redraw = True
  161. if "text" in kwargs:
  162. self._text = kwargs.pop("text")
  163. self._label.configure(text=self._text)
  164. if "font" in kwargs:
  165. if isinstance(self._font, CTkFont):
  166. self._font.remove_size_configure_callback(self._update_font)
  167. self._font = self._check_font_type(kwargs.pop("font"))
  168. if isinstance(self._font, CTkFont):
  169. self._font.add_size_configure_callback(self._update_font)
  170. self._update_font()
  171. if "image" in kwargs:
  172. if isinstance(self._image, CTkImage):
  173. self._image.remove_configure_callback(self._update_image)
  174. self._image = self._check_image_type(kwargs.pop("image"))
  175. if isinstance(self._image, CTkImage):
  176. self._image.add_configure_callback(self._update_image)
  177. self._update_image()
  178. if "compound" in kwargs:
  179. self._compound = kwargs.pop("compound")
  180. self._label.configure(compound=self._compound)
  181. if "anchor" in kwargs:
  182. self._anchor = kwargs.pop("anchor")
  183. self._label.configure(anchor=self._anchor)
  184. self._create_grid()
  185. if "wraplength" in kwargs:
  186. self._wraplength = kwargs.pop("wraplength")
  187. self._label.configure(wraplength=self._apply_widget_scaling(self._wraplength))
  188. self._label.configure(**pop_from_dict_by_set(kwargs, self._valid_tk_label_attributes)) # configure tkinter.Label
  189. super().configure(require_redraw=require_redraw, **kwargs) # configure CTkBaseClass
  190. def cget(self, attribute_name: str) -> any:
  191. if attribute_name == "corner_radius":
  192. return self._corner_radius
  193. elif attribute_name == "fg_color":
  194. return self._fg_color
  195. elif attribute_name == "text_color":
  196. return self._text_color
  197. elif attribute_name == "text_color_disabled":
  198. return self._text_color_disabled
  199. elif attribute_name == "text":
  200. return self._text
  201. elif attribute_name == "font":
  202. return self._font
  203. elif attribute_name == "image":
  204. return self._image
  205. elif attribute_name == "compound":
  206. return self._compound
  207. elif attribute_name == "anchor":
  208. return self._anchor
  209. elif attribute_name == "wraplength":
  210. return self._wraplength
  211. elif attribute_name in self._valid_tk_label_attributes:
  212. return self._label.cget(attribute_name) # cget of tkinter.Label
  213. else:
  214. return super().cget(attribute_name) # cget of CTkBaseClass
  215. def bind(self, sequence: str = None, command: Callable = None, add: str = True):
  216. """ called on the tkinter.Label and tkinter.Canvas """
  217. if not (add == "+" or add is True):
  218. raise ValueError("'add' argument can only be '+' or True to preserve internal callbacks")
  219. self._canvas.bind(sequence, command, add=True)
  220. self._label.bind(sequence, command, add=True)
  221. def unbind(self, sequence: str = None, funcid: Optional[str] = None):
  222. """ called on the tkinter.Label and tkinter.Canvas """
  223. if funcid is not None:
  224. raise ValueError("'funcid' argument can only be None, because there is a bug in" +
  225. " tkinter and its not clear whether the internal callbacks will be unbinded or not")
  226. self._canvas.unbind(sequence, None)
  227. self._label.unbind(sequence, None)
  228. def focus(self):
  229. return self._label.focus()
  230. def focus_set(self):
  231. return self._label.focus_set()
  232. def focus_force(self):
  233. return self._label.focus_force()