ctk_button.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. import tkinter
  2. import sys
  3. from typing import Union, Tuple, Callable, Optional, Any
  4. from .core_rendering import CTkCanvas
  5. from .theme import ThemeManager
  6. from .core_rendering import DrawEngine
  7. from .core_widget_classes import CTkBaseClass
  8. from .font import CTkFont
  9. from .image import CTkImage
  10. class CTkButton(CTkBaseClass):
  11. """
  12. Button with rounded corners, border, hover effect, image support, click command and textvariable.
  13. For detailed information check out the documentation.
  14. """
  15. _image_label_spacing: int = 6
  16. def __init__(self,
  17. master: Any,
  18. width: int = 140,
  19. height: int = 28,
  20. corner_radius: Optional[int] = None,
  21. border_width: Optional[int] = None,
  22. border_spacing: int = 2,
  23. bg_color: Union[str, Tuple[str, str]] = "transparent",
  24. fg_color: Optional[Union[str, Tuple[str, str]]] = None,
  25. hover_color: Optional[Union[str, Tuple[str, str]]] = None,
  26. border_color: Optional[Union[str, Tuple[str, str]]] = None,
  27. text_color: Optional[Union[str, Tuple[str, str]]] = None,
  28. text_color_disabled: Optional[Union[str, Tuple[str, str]]] = None,
  29. background_corner_colors: Union[Tuple[Union[str, Tuple[str, str]]], None] = None,
  30. round_width_to_even_numbers: bool = True,
  31. round_height_to_even_numbers: bool = True,
  32. text: str = "CTkButton",
  33. font: Optional[Union[tuple, CTkFont]] = None,
  34. textvariable: Union[tkinter.Variable, None] = None,
  35. image: Union[CTkImage, "ImageTk.PhotoImage", None] = None,
  36. state: str = "normal",
  37. hover: bool = True,
  38. command: Union[Callable[[], Any], None] = None,
  39. compound: str = "left",
  40. anchor: str = "center",
  41. **kwargs):
  42. # transfer basic functionality (bg_color, size, appearance_mode, scaling) to CTkBaseClass
  43. super().__init__(master=master, bg_color=bg_color, width=width, height=height, **kwargs)
  44. # shape
  45. self._corner_radius: int = ThemeManager.theme["CTkButton"]["corner_radius"] if corner_radius is None else corner_radius
  46. self._corner_radius = min(self._corner_radius, round(self._current_height / 2))
  47. self._border_width: int = ThemeManager.theme["CTkButton"]["border_width"] if border_width is None else border_width
  48. self._border_spacing: int = border_spacing
  49. # color
  50. self._fg_color: Union[str, Tuple[str, str]] = ThemeManager.theme["CTkButton"]["fg_color"] if fg_color is None else self._check_color_type(fg_color, transparency=True)
  51. self._hover_color: Union[str, Tuple[str, str]] = ThemeManager.theme["CTkButton"]["hover_color"] if hover_color is None else self._check_color_type(hover_color)
  52. self._border_color: Union[str, Tuple[str, str]] = ThemeManager.theme["CTkButton"]["border_color"] if border_color is None else self._check_color_type(border_color)
  53. self._text_color: Union[str, Tuple[str, str]] = ThemeManager.theme["CTkButton"]["text_color"] if text_color is None else self._check_color_type(text_color)
  54. self._text_color_disabled: Union[str, Tuple[str, str]] = ThemeManager.theme["CTkButton"]["text_color_disabled"] if text_color_disabled is None else self._check_color_type(text_color_disabled)
  55. # rendering options
  56. self._background_corner_colors: Union[Tuple[Union[str, Tuple[str, str]]], None] = background_corner_colors # rendering options for DrawEngine
  57. self._round_width_to_even_numbers: bool = round_width_to_even_numbers # rendering options for DrawEngine
  58. self._round_height_to_even_numbers: bool = round_height_to_even_numbers # rendering options for DrawEngine
  59. # text, font
  60. self._text = text
  61. self._text_label: Union[tkinter.Label, None] = None
  62. self._textvariable: tkinter.Variable = textvariable
  63. self._font: Union[tuple, CTkFont] = CTkFont() if font is None else self._check_font_type(font)
  64. if isinstance(self._font, CTkFont):
  65. self._font.add_size_configure_callback(self._update_font)
  66. # image
  67. self._image = self._check_image_type(image)
  68. self._image_label: Union[tkinter.Label, None] = None
  69. if isinstance(self._image, CTkImage):
  70. self._image.add_configure_callback(self._update_image)
  71. # other
  72. self._state: str = state
  73. self._hover: bool = hover
  74. self._command: Callable = command
  75. self._compound: str = compound
  76. self._anchor: str = anchor
  77. self._click_animation_running: bool = False
  78. # canvas and draw engine
  79. self._canvas = CTkCanvas(master=self,
  80. highlightthickness=0,
  81. width=self._apply_widget_scaling(self._desired_width),
  82. height=self._apply_widget_scaling(self._desired_height))
  83. self._canvas.grid(row=0, column=0, rowspan=5, columnspan=5, sticky="nsew")
  84. self._draw_engine = DrawEngine(self._canvas)
  85. self._draw_engine.set_round_to_even_numbers(self._round_width_to_even_numbers, self._round_height_to_even_numbers) # rendering options
  86. # configure cursor and initial draw
  87. self._create_bindings()
  88. self._set_cursor()
  89. self._draw()
  90. def _create_bindings(self, sequence: Optional[str] = None):
  91. """ set necessary bindings for functionality of widget, will overwrite other bindings """
  92. if sequence is None or sequence == "<Enter>":
  93. self._canvas.bind("<Enter>", self._on_enter)
  94. if self._text_label is not None:
  95. self._text_label.bind("<Enter>", self._on_enter)
  96. if self._image_label is not None:
  97. self._image_label.bind("<Enter>", self._on_enter)
  98. if sequence is None or sequence == "<Leave>":
  99. self._canvas.bind("<Leave>", self._on_leave)
  100. if self._text_label is not None:
  101. self._text_label.bind("<Leave>", self._on_leave)
  102. if self._image_label is not None:
  103. self._image_label.bind("<Leave>", self._on_leave)
  104. if sequence is None or sequence == "<Button-1>":
  105. self._canvas.bind("<Button-1>", self._clicked)
  106. if self._text_label is not None:
  107. self._text_label.bind("<Button-1>", self._clicked)
  108. if self._image_label is not None:
  109. self._image_label.bind("<Button-1>", self._clicked)
  110. def _set_scaling(self, *args, **kwargs):
  111. super()._set_scaling(*args, **kwargs)
  112. self._create_grid()
  113. if self._text_label is not None:
  114. self._text_label.configure(font=self._apply_font_scaling(self._font))
  115. self._update_image()
  116. self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
  117. height=self._apply_widget_scaling(self._desired_height))
  118. self._draw(no_color_updates=True)
  119. def _set_appearance_mode(self, mode_string):
  120. super()._set_appearance_mode(mode_string)
  121. self._update_image()
  122. def _set_dimensions(self, width: int = None, height: int = None):
  123. super()._set_dimensions(width, height)
  124. self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
  125. height=self._apply_widget_scaling(self._desired_height))
  126. self._draw()
  127. def _update_font(self):
  128. """ pass font to tkinter widgets with applied font scaling and update grid with workaround """
  129. if self._text_label is not None:
  130. self._text_label.configure(font=self._apply_font_scaling(self._font))
  131. # Workaround to force grid to be resized when text changes size.
  132. # Otherwise grid will lag and only resizes if other mouse action occurs.
  133. self._canvas.grid_forget()
  134. self._canvas.grid(row=0, column=0, rowspan=5, columnspan=5, sticky="nsew")
  135. def _update_image(self):
  136. if self._image_label is not None:
  137. if isinstance(self._image, CTkImage):
  138. self._image_label.configure(image=self._image.create_scaled_photo_image(self._get_widget_scaling(),
  139. self._get_appearance_mode()))
  140. elif self._image is not None:
  141. self._image_label.configure(image=self._image)
  142. def destroy(self):
  143. if isinstance(self._font, CTkFont):
  144. self._font.remove_size_configure_callback(self._update_font)
  145. super().destroy()
  146. def _draw(self, no_color_updates=False):
  147. super()._draw(no_color_updates)
  148. if self._background_corner_colors is not None:
  149. self._draw_engine.draw_background_corners(self._apply_widget_scaling(self._current_width),
  150. self._apply_widget_scaling(self._current_height))
  151. self._canvas.itemconfig("background_corner_top_left", fill=self._apply_appearance_mode(self._background_corner_colors[0]))
  152. self._canvas.itemconfig("background_corner_top_right", fill=self._apply_appearance_mode(self._background_corner_colors[1]))
  153. self._canvas.itemconfig("background_corner_bottom_right", fill=self._apply_appearance_mode(self._background_corner_colors[2]))
  154. self._canvas.itemconfig("background_corner_bottom_left", fill=self._apply_appearance_mode(self._background_corner_colors[3]))
  155. else:
  156. self._canvas.delete("background_parts")
  157. requires_recoloring = self._draw_engine.draw_rounded_rect_with_border(self._apply_widget_scaling(self._current_width),
  158. self._apply_widget_scaling(self._current_height),
  159. self._apply_widget_scaling(self._corner_radius),
  160. self._apply_widget_scaling(self._border_width))
  161. if no_color_updates is False or requires_recoloring:
  162. self._canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
  163. # set color for the button border parts (outline)
  164. self._canvas.itemconfig("border_parts",
  165. outline=self._apply_appearance_mode(self._border_color),
  166. fill=self._apply_appearance_mode(self._border_color))
  167. # set color for inner button parts
  168. if self._fg_color == "transparent":
  169. self._canvas.itemconfig("inner_parts",
  170. outline=self._apply_appearance_mode(self._bg_color),
  171. fill=self._apply_appearance_mode(self._bg_color))
  172. else:
  173. self._canvas.itemconfig("inner_parts",
  174. outline=self._apply_appearance_mode(self._fg_color),
  175. fill=self._apply_appearance_mode(self._fg_color))
  176. # create text label if text given
  177. if self._text is not None and self._text != "":
  178. if self._text_label is None:
  179. self._text_label = tkinter.Label(master=self,
  180. font=self._apply_font_scaling(self._font),
  181. text=self._text,
  182. padx=0,
  183. pady=0,
  184. borderwidth=1,
  185. textvariable=self._textvariable)
  186. self._create_grid()
  187. self._text_label.bind("<Enter>", self._on_enter)
  188. self._text_label.bind("<Leave>", self._on_leave)
  189. self._text_label.bind("<Button-1>", self._clicked)
  190. self._text_label.bind("<Button-1>", self._clicked)
  191. if no_color_updates is False:
  192. # set text_label fg color (text color)
  193. self._text_label.configure(fg=self._apply_appearance_mode(self._text_color))
  194. if self._state == tkinter.DISABLED:
  195. self._text_label.configure(fg=(self._apply_appearance_mode(self._text_color_disabled)))
  196. else:
  197. self._text_label.configure(fg=self._apply_appearance_mode(self._text_color))
  198. if self._apply_appearance_mode(self._fg_color) == "transparent":
  199. self._text_label.configure(bg=self._apply_appearance_mode(self._bg_color))
  200. else:
  201. self._text_label.configure(bg=self._apply_appearance_mode(self._fg_color))
  202. else:
  203. # delete text_label if no text given
  204. if self._text_label is not None:
  205. self._text_label.destroy()
  206. self._text_label = None
  207. self._create_grid()
  208. # create image label if image given
  209. if self._image is not None:
  210. if self._image_label is None:
  211. self._image_label = tkinter.Label(master=self)
  212. self._update_image() # set image
  213. self._create_grid()
  214. self._image_label.bind("<Enter>", self._on_enter)
  215. self._image_label.bind("<Leave>", self._on_leave)
  216. self._image_label.bind("<Button-1>", self._clicked)
  217. self._image_label.bind("<Button-1>", self._clicked)
  218. if no_color_updates is False:
  219. # set image_label bg color (background color of label)
  220. if self._apply_appearance_mode(self._fg_color) == "transparent":
  221. self._image_label.configure(bg=self._apply_appearance_mode(self._bg_color))
  222. else:
  223. self._image_label.configure(bg=self._apply_appearance_mode(self._fg_color))
  224. else:
  225. # delete text_label if no text given
  226. if self._image_label is not None:
  227. self._image_label.destroy()
  228. self._image_label = None
  229. self._create_grid()
  230. def _create_grid(self):
  231. """ configure grid system (5x5) """
  232. # Outer rows and columns have weight of 1000 to overpower the rows and columns of the label and image with weight 1.
  233. # Rows and columns of image and label need weight of 1 to collapse in case of missing space on the button,
  234. # so image and label need sticky option to stick together in the center, and therefore outer rows and columns
  235. # need weight of 100 in case of other anchor than center.
  236. n_padding_weight, s_padding_weight, e_padding_weight, w_padding_weight = 1000, 1000, 1000, 1000
  237. if self._anchor != "center":
  238. if "n" in self._anchor:
  239. n_padding_weight, s_padding_weight = 0, 1000
  240. if "s" in self._anchor:
  241. n_padding_weight, s_padding_weight = 1000, 0
  242. if "e" in self._anchor:
  243. e_padding_weight, w_padding_weight = 1000, 0
  244. if "w" in self._anchor:
  245. e_padding_weight, w_padding_weight = 0, 1000
  246. scaled_minsize_rows = self._apply_widget_scaling(max(self._border_width + 1, self._border_spacing))
  247. scaled_minsize_columns = self._apply_widget_scaling(max(self._corner_radius, self._border_width + 1, self._border_spacing))
  248. self.grid_rowconfigure(0, weight=n_padding_weight, minsize=scaled_minsize_rows)
  249. self.grid_rowconfigure(4, weight=s_padding_weight, minsize=scaled_minsize_rows)
  250. self.grid_columnconfigure(0, weight=e_padding_weight, minsize=scaled_minsize_columns)
  251. self.grid_columnconfigure(4, weight=w_padding_weight, minsize=scaled_minsize_columns)
  252. if self._compound in ("right", "left"):
  253. self.grid_rowconfigure(2, weight=1)
  254. if self._image_label is not None and self._text_label is not None:
  255. self.grid_columnconfigure(2, weight=0, minsize=self._apply_widget_scaling(self._image_label_spacing))
  256. else:
  257. self.grid_columnconfigure(2, weight=0)
  258. self.grid_rowconfigure((1, 3), weight=0)
  259. self.grid_columnconfigure((1, 3), weight=1)
  260. else:
  261. self.grid_columnconfigure(2, weight=1)
  262. if self._image_label is not None and self._text_label is not None:
  263. self.grid_rowconfigure(2, weight=0, minsize=self._apply_widget_scaling(self._image_label_spacing))
  264. else:
  265. self.grid_rowconfigure(2, weight=0)
  266. self.grid_columnconfigure((1, 3), weight=0)
  267. self.grid_rowconfigure((1, 3), weight=1)
  268. if self._compound == "right":
  269. if self._image_label is not None:
  270. self._image_label.grid(row=2, column=3, sticky="w")
  271. if self._text_label is not None:
  272. self._text_label.grid(row=2, column=1, sticky="e")
  273. elif self._compound == "left":
  274. if self._image_label is not None:
  275. self._image_label.grid(row=2, column=1, sticky="e")
  276. if self._text_label is not None:
  277. self._text_label.grid(row=2, column=3, sticky="w")
  278. elif self._compound == "top":
  279. if self._image_label is not None:
  280. self._image_label.grid(row=1, column=2, sticky="s")
  281. if self._text_label is not None:
  282. self._text_label.grid(row=3, column=2, sticky="n")
  283. elif self._compound == "bottom":
  284. if self._image_label is not None:
  285. self._image_label.grid(row=3, column=2, sticky="n")
  286. if self._text_label is not None:
  287. self._text_label.grid(row=1, column=2, sticky="s")
  288. def configure(self, require_redraw=False, **kwargs):
  289. if "corner_radius" in kwargs:
  290. self._corner_radius = kwargs.pop("corner_radius")
  291. self._create_grid()
  292. require_redraw = True
  293. if "border_width" in kwargs:
  294. self._border_width = kwargs.pop("border_width")
  295. self._create_grid()
  296. require_redraw = True
  297. if "border_spacing" in kwargs:
  298. self._border_spacing = kwargs.pop("border_spacing")
  299. self._create_grid()
  300. require_redraw = True
  301. if "fg_color" in kwargs:
  302. self._fg_color = self._check_color_type(kwargs.pop("fg_color"), transparency=True)
  303. require_redraw = True
  304. if "hover_color" in kwargs:
  305. self._hover_color = self._check_color_type(kwargs.pop("hover_color"))
  306. require_redraw = True
  307. if "border_color" in kwargs:
  308. self._border_color = self._check_color_type(kwargs.pop("border_color"))
  309. require_redraw = True
  310. if "text_color" in kwargs:
  311. self._text_color = self._check_color_type(kwargs.pop("text_color"))
  312. require_redraw = True
  313. if "text_color_disabled" in kwargs:
  314. self._text_color_disabled = self._check_color_type(kwargs.pop("text_color_disabled"))
  315. require_redraw = True
  316. if "background_corner_colors" in kwargs:
  317. self._background_corner_colors = kwargs.pop("background_corner_colors")
  318. require_redraw = True
  319. if "text" in kwargs:
  320. self._text = kwargs.pop("text")
  321. if self._text_label is None:
  322. require_redraw = True # text_label will be created in .draw()
  323. else:
  324. self._text_label.configure(text=self._text)
  325. if "font" in kwargs:
  326. if isinstance(self._font, CTkFont):
  327. self._font.remove_size_configure_callback(self._update_font)
  328. self._font = self._check_font_type(kwargs.pop("font"))
  329. if isinstance(self._font, CTkFont):
  330. self._font.add_size_configure_callback(self._update_font)
  331. self._update_font()
  332. if "textvariable" in kwargs:
  333. self._textvariable = kwargs.pop("textvariable")
  334. if self._text_label is not None:
  335. self._text_label.configure(textvariable=self._textvariable)
  336. if "image" in kwargs:
  337. if isinstance(self._image, CTkImage):
  338. self._image.remove_configure_callback(self._update_image)
  339. self._image = self._check_image_type(kwargs.pop("image"))
  340. if isinstance(self._image, CTkImage):
  341. self._image.add_configure_callback(self._update_image)
  342. self._update_image()
  343. if "state" in kwargs:
  344. self._state = kwargs.pop("state")
  345. self._set_cursor()
  346. require_redraw = True
  347. if "hover" in kwargs:
  348. self._hover = kwargs.pop("hover")
  349. if "command" in kwargs:
  350. self._command = kwargs.pop("command")
  351. self._set_cursor()
  352. if "compound" in kwargs:
  353. self._compound = kwargs.pop("compound")
  354. require_redraw = True
  355. if "anchor" in kwargs:
  356. self._anchor = kwargs.pop("anchor")
  357. self._create_grid()
  358. require_redraw = True
  359. super().configure(require_redraw=require_redraw, **kwargs)
  360. def cget(self, attribute_name: str) -> any:
  361. if attribute_name == "corner_radius":
  362. return self._corner_radius
  363. elif attribute_name == "border_width":
  364. return self._border_width
  365. elif attribute_name == "border_spacing":
  366. return self._border_spacing
  367. elif attribute_name == "fg_color":
  368. return self._fg_color
  369. elif attribute_name == "hover_color":
  370. return self._hover_color
  371. elif attribute_name == "border_color":
  372. return self._border_color
  373. elif attribute_name == "text_color":
  374. return self._text_color
  375. elif attribute_name == "text_color_disabled":
  376. return self._text_color_disabled
  377. elif attribute_name == "background_corner_colors":
  378. return self._background_corner_colors
  379. elif attribute_name == "text":
  380. return self._text
  381. elif attribute_name == "font":
  382. return self._font
  383. elif attribute_name == "textvariable":
  384. return self._textvariable
  385. elif attribute_name == "image":
  386. return self._image
  387. elif attribute_name == "state":
  388. return self._state
  389. elif attribute_name == "hover":
  390. return self._hover
  391. elif attribute_name == "command":
  392. return self._command
  393. elif attribute_name == "compound":
  394. return self._compound
  395. elif attribute_name == "anchor":
  396. return self._anchor
  397. else:
  398. return super().cget(attribute_name)
  399. def _set_cursor(self):
  400. if self._cursor_manipulation_enabled:
  401. if self._state == tkinter.DISABLED:
  402. if sys.platform == "darwin" and self._command is not None:
  403. self.configure(cursor="arrow")
  404. elif sys.platform.startswith("win") and self._command is not None:
  405. self.configure(cursor="arrow")
  406. elif self._state == tkinter.NORMAL:
  407. if sys.platform == "darwin" and self._command is not None:
  408. self.configure(cursor="pointinghand")
  409. elif sys.platform.startswith("win") and self._command is not None:
  410. self.configure(cursor="hand2")
  411. def _on_enter(self, event=None):
  412. if self._hover is True and self._state == "normal":
  413. if self._hover_color is None:
  414. inner_parts_color = self._fg_color
  415. else:
  416. inner_parts_color = self._hover_color
  417. # set color of inner button parts to hover color
  418. self._canvas.itemconfig("inner_parts",
  419. outline=self._apply_appearance_mode(inner_parts_color),
  420. fill=self._apply_appearance_mode(inner_parts_color))
  421. # set text_label bg color to button hover color
  422. if self._text_label is not None:
  423. self._text_label.configure(bg=self._apply_appearance_mode(inner_parts_color))
  424. # set image_label bg color to button hover color
  425. if self._image_label is not None:
  426. self._image_label.configure(bg=self._apply_appearance_mode(inner_parts_color))
  427. def _on_leave(self, event=None):
  428. self._click_animation_running = False
  429. if self._fg_color == "transparent":
  430. inner_parts_color = self._bg_color
  431. else:
  432. inner_parts_color = self._fg_color
  433. # set color of inner button parts
  434. self._canvas.itemconfig("inner_parts",
  435. outline=self._apply_appearance_mode(inner_parts_color),
  436. fill=self._apply_appearance_mode(inner_parts_color))
  437. # set text_label bg color (label color)
  438. if self._text_label is not None:
  439. self._text_label.configure(bg=self._apply_appearance_mode(inner_parts_color))
  440. # set image_label bg color (image bg color)
  441. if self._image_label is not None:
  442. self._image_label.configure(bg=self._apply_appearance_mode(inner_parts_color))
  443. def _click_animation(self):
  444. if self._click_animation_running:
  445. self._on_enter()
  446. def _clicked(self, event=None):
  447. if self._state != tkinter.DISABLED:
  448. # click animation: change color with .on_leave() and back to normal after 100ms with click_animation()
  449. self._on_leave()
  450. self._click_animation_running = True
  451. self.after(100, self._click_animation)
  452. if self._command is not None:
  453. self._command()
  454. def invoke(self):
  455. """ calls command function if button is not disabled """
  456. if self._state != tkinter.DISABLED:
  457. if self._command is not None:
  458. return self._command()
  459. def bind(self, sequence: str = None, command: Callable = None, add: Union[str, bool] = True):
  460. """ called on the tkinter.Canvas """
  461. if not (add == "+" or add is True):
  462. raise ValueError("'add' argument can only be '+' or True to preserve internal callbacks")
  463. self._canvas.bind(sequence, command, add=True)
  464. if self._text_label is not None:
  465. self._text_label.bind(sequence, command, add=True)
  466. if self._image_label is not None:
  467. self._image_label.bind(sequence, command, add=True)
  468. def unbind(self, sequence: str = None, funcid: str = None):
  469. """ called on the tkinter.Label and tkinter.Canvas """
  470. if funcid is not None:
  471. raise ValueError("'funcid' argument can only be None, because there is a bug in" +
  472. " tkinter and its not clear whether the internal callbacks will be unbinded or not")
  473. self._canvas.unbind(sequence, None)
  474. if self._text_label is not None:
  475. self._text_label.unbind(sequence, None)
  476. if self._image_label is not None:
  477. self._image_label.unbind(sequence, None)
  478. self._create_bindings(sequence=sequence) # restore internal callbacks for sequence
  479. def focus(self):
  480. return self._text_label.focus()
  481. def focus_set(self):
  482. return self._text_label.focus_set()
  483. def focus_force(self):
  484. return self._text_label.focus_force()