Changing font size of a button #121
Unanswered
Aniket360-coder
asked this question in
Q&A
Replies: 1 comment
-
I have tested the button and I couldn't change the font size too. # this method was added to CTkButton, I won't repeat all the code for simplify it
def set_text_size(self, size):
# this is needed because the text can be None or ""
# it happens if the bt has an image
if type(self.text_label).__name__ == "Label":
# default is -13
# get saved font family
font_family = ThemeManager.theme["text"]["font"]
# pass a newly created tuple with the current font family and new font size
self.text_font = (font_family, str(size))
# destroy previous self.text.label
self.text_label.destroy()
# create a new text label with the new font size
self.text_label = tkinter.Label(master=self,
font=self.text_font,
textvariable=self.textvariable)
# we need to bind text_label again because
# it can receive a new id from tkinter
self.text_label.bind("<Enter>", self.on_enter)
self.text_label.bind("<Leave>", self.on_leave)
self.text_label.bind("<Button-1>", self.clicked)
self.text_label.bind("<Button-1>", self.clicked) and I tested using this code: import tkinter
import customtkinter
from customtkinter.theme_manager import ThemeManager
DARK_MODE = "dark"
customtkinter.set_appearance_mode(DARK_MODE)
customtkinter.set_default_color_theme("blue")
class App(customtkinter.CTk):
def props(self, it):
print("Inner attributes from ", str(it))
print(str([i for i in it.__dict__.keys() if i[:1] != '_']))
print("__________________")
def attr_change(self):
self.props(self.bt)
self.bt.set_text_size(42)
self.bt.set_text("1")
self.bt.update()
def __init__(self):
super().__init__()
self.title("Change Attribute")
self.geometry("800x600")
# contains everything
main_container = customtkinter.CTkFrame(self)
main_container.pack(fill=tkinter.BOTH, expand=True, padx=10, pady=10)
self.bt = customtkinter.CTkButton(main_container, text="Hi", text_font=("Courier", 12), command=self.attr_change)
self.bt.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
a = App()
a.mainloop() But I don't know if this is a good way to do this, since I'm destroying the button label and replacing it. If we need to change 10 bts we will need to destroy and create 10 new labels. It seems wrong. I'm also not sure if it's going to preserve the theme styles. And I found a strange behavior for hover, enter and leave states, that's the why for adding the events again. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How to change the font size of a button text after the button is created
Beta Was this translation helpful? Give feedback.
All reactions