Akascape / CTkListbox

A simple listbox for customtkinter (extenstion/add-on)
MIT License
158 stars 13 forks source link

CTkListbox 'font' parameter not working #71

Open ItsMichaelll opened 2 months ago

ItsMichaelll commented 2 months ago

I wanted to change the font of the buttons inserted into the CTkListbox, so I used the 'font' parameter, but it wasn't changing the font of the buttons.

Here's my initialization of the CTkListbox:

list = CTkListbox(
    master=self.frame1,
    height=200,
    text_color=['#000, #FFF'],
    font=("Proxima Nova Rg", 18, "bold"), # this wasn't doing anything
    hover_color='#00458A',
    highlight_color='#0267c9',
    multiple_selection=True,
)

I discovered a temporary fix for myself by modifying your ctk_listbox.py file, particularly the 'insert' method in line 198. I did have to add the import line import ast to the top of your ctk_listbox.py file for my fix, though.

'ctk_listbox.py' (line 198):

    def insert(self, index, option, update=True, **args):
        """add new option in the listbox"""

        if str(index).lower() == "end":
            index = f"END{self.end_num}"
            self.end_num += 1

        if index in self.buttons:
            self.buttons[index].destroy()

        font = ast.literal_eval(self.font.cget('family'))    # i added this

        self.buttons[index] = customtkinter.CTkButton(
            self,
            text=option,
            fg_color=self.button_fg_color,
            anchor=self.justify,
            text_color=self.text_color,
            font=font,    # i changed this from 'font=self.font' to 'font=font'
            hover_color=self.hover_color,
            **args,
        )
        # the rest of the method stays the same

I'm hoping there is a better fix for this? Let me know, and if you're having trouble reproducing it I can provide more of my code.

gioradicci commented 3 weeks ago

It doesn't work instancing the class with font parameter, but it works if assigned after with the font property : Example: ` custom_font =("Arial",11) self.listBoxAnnoTarget = CTkListbox(self.targetoptselection_frame, multiple_selection=False, command=self.filter_ui, font=custom_font) #NO CHANGE

self.listBoxAnnoTarget = CTkListbox(self.targetoptselection_frame, multiple_selection=False, command=self.filter_ui) self.listBoxAnnoTarget.font=custom_font #THIS WORKS ` This is the workaround I used.