CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

CtkButton image centering wrong after adding and removing text

Open Protarios opened this issue 2 years ago • 4 comments

If I add text to a CtkButton via configure and remove it again through setting an empty string the centering ist wrong If added some small code below. As you can see Button 2 has a more "lefty" centering after adding and removing text than the button above.

import customtkinter as ctk
import tkinter as tk
from PIL import Image, ImageTk

# window 
window = ctk.CTk()
window.title('customtkinter app')
window.geometry('200x200')

frame = ctk.CTkFrame(window, fg_color='grey', width=50, corner_radius=0)
frame.grid(column=0, row=0, sticky='nsew')
frame.columnconfigure(0, weight=1)

img_butt = ctk.CTkImage(Image.open('python.png'),size=(40, 40))
img_butt2 = ctk.CTkImage(Image.open('python.png'),size=(40, 40))
home_b = ctk.CTkButton(master=frame, text='', image=img_butt, corner_radius=0, anchor='c')
home_b.grid(row = 0, column = 0, sticky = 'nesw')
home_b2 = ctk.CTkButton(master=frame, text='', image=img_butt2, corner_radius=0, anchor='c')
home_b2.grid(row = 1, column = 0, sticky = 'nesw')
home_b2.configure(text='Test', anchor='w')
home_b2.configure(text='', anchor='c')

window.mainloop()

Output of sample code

python.png

Protarios avatar Aug 10 '23 15:08 Protarios

I was able to successfully recreate your issue. If I had to guess the problem, it would probably be that it adds a text box with padding into the button. Then, when you clear the text box, the text box is still there with the padding, shifting the image just a little bit.

That's just a guess though. I'm going to go through the source code and see if I can discover the problem and make a pull request.

DragonOfShuu avatar Aug 22 '23 22:08 DragonOfShuu

I found the solution and created a pull request! Basically, when you added the text, a column was given a minsize, but when you removed the text this was not reset. This took a while to find so hopefully it gets pulled in!

        if self._compound in ("right", "left"):
            self.grid_rowconfigure(2, weight=1)
            if self._image_label is not None and self._text_label is not None:
                self.grid_columnconfigure(2, weight=0, minsize=self._apply_widget_scaling(self._image_label_spacing))
            else:
                self.grid_columnconfigure(2, weight=0, minsize=0) # <- Now resets

            self.grid_rowconfigure((1, 3), weight=0)
            self.grid_columnconfigure((1, 3), weight=1)
        else:
            self.grid_columnconfigure(2, weight=1)
            if self._image_label is not None and self._text_label is not None:
                self.grid_rowconfigure(2, weight=0, minsize=self._apply_widget_scaling(self._image_label_spacing))
            else:
                self.grid_rowconfigure(2, weight=0, minsize=0) # <- Now resets

            self.grid_columnconfigure((1, 3), weight=0)
            self.grid_rowconfigure((1, 3), weight=1)

DragonOfShuu avatar Aug 23 '23 00:08 DragonOfShuu

Great! I am currently using a custom local version including this fix.

Protarios avatar Oct 13 '23 12:10 Protarios

Great! I am currently using a custom local version including this fix.

Lol and I came back a year later, and ig they don't accept PRs. Good thing you are using a local version lol

DragonOfShuu avatar Sep 14 '24 17:09 DragonOfShuu