From ea7a5618a0a93aa4bfba28fd4b9108202664b536 Mon Sep 17 00:00:00 2001 From: Morgan Thibert Date: Thu, 4 Nov 2021 16:20:08 +0100 Subject: [PATCH] [functionality] add pack support for position of customCTkButton --- customtkinter/customtkinter_button.py | 31 ++++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/customtkinter/customtkinter_button.py b/customtkinter/customtkinter_button.py index d763710..3b1f0a6 100644 --- a/customtkinter/customtkinter_button.py +++ b/customtkinter/customtkinter_button.py @@ -30,6 +30,8 @@ class CTkButton(tkinter.Frame): AppearanceModeTracker.add(self.set_appearance_mode) + self.gridConfigure() + if bg_color is None: if isinstance(self.master, CTkFrame): self.bg_color = self.master.fg_color @@ -49,10 +51,10 @@ class CTkButton(tkinter.Frame): self.width = width self.height = height - if corner_radius*2 > self.height: - self.corner_radius = self.height/2 - elif corner_radius*2 > self.width: - self.corner_radius = self.width/2 + if corner_radius * 2 > self.height: + self.corner_radius = self.height / 2 + elif corner_radius * 2 > self.width: + self.corner_radius = self.width / 2 else: self.corner_radius = corner_radius @@ -89,7 +91,7 @@ class CTkButton(tkinter.Frame): highlightthicknes=0, width=self.width, height=self.height) - self.canvas.place(x=0, y=0) + self.canvas.grid(row=0, column=0) if self.hover is True: self.canvas.bind("", self.on_enter) @@ -103,6 +105,20 @@ class CTkButton(tkinter.Frame): self.text_label = None self.image_label = None + # Each time an item is resized due to pack position mode, the binding Configure is called on the widget + self.bind('', self.updateDimensions) + self.draw() + + def gridConfigure(self): + # Configuration of a basic grid in which all elements of CTkButtons are centered on one row and one column + self.grid_rowconfigure(0, weight=1) + self.grid_columnconfigure(0, weight=1) + + def updateDimensions(self, event): + # We update the dimensions of the internal elements of CTkButton Widget + self.canvas.config(width=event.width, height=event.height) + self.width = event.width + self.height = event.height self.draw() def draw(self): @@ -200,7 +216,7 @@ class CTkButton(tkinter.Frame): self.text_label = tkinter.Label(master=self, text=self.text, font=self.text_font) - self.text_label.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER) + self.text_label.grid(row=0, column=0) self.text_label.bind("", self.on_enter) self.text_label.bind("", self.on_leave) @@ -230,7 +246,7 @@ class CTkButton(tkinter.Frame): else: self.image_label = tkinter.Label(master=self, image=self.image) - self.image_label.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER) + self.image_label.grid(row=0, column=0) self.image_label.bind("", self.on_enter) self.image_label.bind("", self.on_leave) @@ -362,4 +378,3 @@ class CTkButton(tkinter.Frame): self.appearance_mode = 0 self.draw() -