ctk_frame.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. from typing import Union, Tuple, List, Optional, Any
  2. from .core_rendering import CTkCanvas
  3. from .theme import ThemeManager
  4. from .core_rendering import DrawEngine
  5. from .core_widget_classes import CTkBaseClass
  6. class CTkFrame(CTkBaseClass):
  7. """
  8. Frame with rounded corners and border.
  9. Default foreground colors are set according to theme.
  10. To make the frame transparent set fg_color=None.
  11. For detailed information check out the documentation.
  12. """
  13. def __init__(self,
  14. master: Any,
  15. width: int = 200,
  16. height: int = 200,
  17. corner_radius: Optional[Union[int, str]] = None,
  18. border_width: Optional[Union[int, str]] = None,
  19. bg_color: Union[str, Tuple[str, str]] = "transparent",
  20. fg_color: Optional[Union[str, Tuple[str, str]]] = None,
  21. border_color: Optional[Union[str, Tuple[str, str]]] = None,
  22. background_corner_colors: Union[Tuple[Union[str, Tuple[str, str]]], None] = None,
  23. overwrite_preferred_drawing_method: Union[str, None] = None,
  24. **kwargs):
  25. # transfer basic functionality (_bg_color, size, __appearance_mode, scaling) to CTkBaseClass
  26. super().__init__(master=master, bg_color=bg_color, width=width, height=height, **kwargs)
  27. # color
  28. self._border_color = ThemeManager.theme["CTkFrame"]["border_color"] if border_color is None else self._check_color_type(border_color)
  29. # determine fg_color of frame
  30. if fg_color is None:
  31. if isinstance(self.master, CTkFrame):
  32. if self.master._fg_color == ThemeManager.theme["CTkFrame"]["fg_color"]:
  33. self._fg_color = ThemeManager.theme["CTkFrame"]["top_fg_color"]
  34. else:
  35. self._fg_color = ThemeManager.theme["CTkFrame"]["fg_color"]
  36. else:
  37. self._fg_color = ThemeManager.theme["CTkFrame"]["fg_color"]
  38. else:
  39. self._fg_color = self._check_color_type(fg_color, transparency=True)
  40. self._background_corner_colors = background_corner_colors # rendering options for DrawEngine
  41. # shape
  42. self._corner_radius = ThemeManager.theme["CTkFrame"]["corner_radius"] if corner_radius is None else corner_radius
  43. self._border_width = ThemeManager.theme["CTkFrame"]["border_width"] if border_width is None else border_width
  44. self._canvas = CTkCanvas(master=self,
  45. highlightthickness=0,
  46. width=self._apply_widget_scaling(self._current_width),
  47. height=self._apply_widget_scaling(self._current_height))
  48. self._canvas.place(x=0, y=0, relwidth=1, relheight=1)
  49. self._canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
  50. self._draw_engine = DrawEngine(self._canvas)
  51. self._overwrite_preferred_drawing_method = overwrite_preferred_drawing_method
  52. self._draw(no_color_updates=True)
  53. def winfo_children(self) -> List[any]:
  54. """
  55. winfo_children of CTkFrame without self.canvas widget,
  56. because it's not a child but part of the CTkFrame itself
  57. """
  58. child_widgets = super().winfo_children()
  59. try:
  60. child_widgets.remove(self._canvas)
  61. return child_widgets
  62. except ValueError:
  63. return child_widgets
  64. def _set_scaling(self, *args, **kwargs):
  65. super()._set_scaling(*args, **kwargs)
  66. self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
  67. height=self._apply_widget_scaling(self._desired_height))
  68. self._draw()
  69. def _set_dimensions(self, width=None, height=None):
  70. super()._set_dimensions(width, height)
  71. self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
  72. height=self._apply_widget_scaling(self._desired_height))
  73. self._draw()
  74. def _draw(self, no_color_updates=False):
  75. super()._draw(no_color_updates)
  76. if not self._canvas.winfo_exists():
  77. return
  78. if self._background_corner_colors is not None:
  79. self._draw_engine.draw_background_corners(self._apply_widget_scaling(self._current_width),
  80. self._apply_widget_scaling(self._current_height))
  81. self._canvas.itemconfig("background_corner_top_left", fill=self._apply_appearance_mode(self._background_corner_colors[0]))
  82. self._canvas.itemconfig("background_corner_top_right", fill=self._apply_appearance_mode(self._background_corner_colors[1]))
  83. self._canvas.itemconfig("background_corner_bottom_right", fill=self._apply_appearance_mode(self._background_corner_colors[2]))
  84. self._canvas.itemconfig("background_corner_bottom_left", fill=self._apply_appearance_mode(self._background_corner_colors[3]))
  85. else:
  86. self._canvas.delete("background_parts")
  87. requires_recoloring = self._draw_engine.draw_rounded_rect_with_border(self._apply_widget_scaling(self._current_width),
  88. self._apply_widget_scaling(self._current_height),
  89. self._apply_widget_scaling(self._corner_radius),
  90. self._apply_widget_scaling(self._border_width),
  91. overwrite_preferred_drawing_method=self._overwrite_preferred_drawing_method)
  92. if no_color_updates is False or requires_recoloring:
  93. if self._fg_color == "transparent":
  94. self._canvas.itemconfig("inner_parts",
  95. fill=self._apply_appearance_mode(self._bg_color),
  96. outline=self._apply_appearance_mode(self._bg_color))
  97. else:
  98. self._canvas.itemconfig("inner_parts",
  99. fill=self._apply_appearance_mode(self._fg_color),
  100. outline=self._apply_appearance_mode(self._fg_color))
  101. self._canvas.itemconfig("border_parts",
  102. fill=self._apply_appearance_mode(self._border_color),
  103. outline=self._apply_appearance_mode(self._border_color))
  104. self._canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
  105. # self._canvas.tag_lower("inner_parts") # maybe unnecessary, I don't know ???
  106. # self._canvas.tag_lower("border_parts")
  107. def configure(self, require_redraw=False, **kwargs):
  108. if "fg_color" in kwargs:
  109. self._fg_color = self._check_color_type(kwargs.pop("fg_color"), transparency=True)
  110. require_redraw = True
  111. # check if CTk widgets are children of the frame and change their bg_color to new frame fg_color
  112. for child in self.winfo_children():
  113. if isinstance(child, CTkBaseClass):
  114. child.configure(bg_color=self._fg_color)
  115. if "bg_color" in kwargs:
  116. # pass bg_color change to children if fg_color is "transparent"
  117. if self._fg_color == "transparent":
  118. for child in self.winfo_children():
  119. if isinstance(child, CTkBaseClass):
  120. child.configure(bg_color=self._fg_color)
  121. if "border_color" in kwargs:
  122. self._border_color = self._check_color_type(kwargs.pop("border_color"))
  123. require_redraw = True
  124. if "background_corner_colors" in kwargs:
  125. self._background_corner_colors = kwargs.pop("background_corner_colors")
  126. require_redraw = True
  127. if "corner_radius" in kwargs:
  128. self._corner_radius = kwargs.pop("corner_radius")
  129. require_redraw = True
  130. if "border_width" in kwargs:
  131. self._border_width = kwargs.pop("border_width")
  132. require_redraw = True
  133. super().configure(require_redraw=require_redraw, **kwargs)
  134. def cget(self, attribute_name: str) -> any:
  135. if attribute_name == "corner_radius":
  136. return self._corner_radius
  137. elif attribute_name == "border_width":
  138. return self._border_width
  139. elif attribute_name == "fg_color":
  140. return self._fg_color
  141. elif attribute_name == "border_color":
  142. return self._border_color
  143. elif attribute_name == "background_corner_colors":
  144. return self._background_corner_colors
  145. else:
  146. return super().cget(attribute_name)
  147. def bind(self, sequence=None, command=None, add=True):
  148. """ called on the tkinter.Canvas """
  149. if not (add == "+" or add is True):
  150. raise ValueError("'add' argument can only be '+' or True to preserve internal callbacks")
  151. self._canvas.bind(sequence, command, add=True)
  152. def unbind(self, sequence=None, funcid=None):
  153. """ called on the tkinter.Canvas """
  154. if funcid is not None:
  155. raise ValueError("'funcid' argument can only be None, because there is a bug in" +
  156. " tkinter and its not clear whether the internal callbacks will be unbinded or not")
  157. self._canvas.unbind(sequence, None)