Search code examples
pythonuser-interfacetkintercustomtkinter

Python Tkinter place the text from button with x and y coordinates


How i can place the text from a button not in the middle of the button rather with coordinates, like x and y?

The problem here is that i can't press the button, when i hover over the label

button = customtkinter.CTkButton(self, text="", width=300, height=100)
button.place(x=10, y=20)

label = customtkinter.CTkLabel(button, text="This is an label")
label.place(x=15, y=25)

Solution

  • As Jan_B said, the solution is to bind mouseclick to the label.

    This can be done following the given example (and replacing your_text, your_width, your_height, x, y and callback_func by your values):

    import customtkinter as ctk
    
    win = ctk.CTk()
    
    label = ctk.CTkLabel(win, text=your_text, width=your_width, height=yout_height, fg_color=ctk.ThemeManager.theme["CTkButton"]["fg_color"], corner_radius=ctk.ThemeManager.theme["CTkButton"]["corner_radius"])
    label.place(x=x, y=x)
    label.bind("<Button-1>", callback_func)
    
    win.mainloop()
    

    If you wonder what fg_color=ctk.ThemeManager.theme["CTkButton"]["fg_color"] and corner_radius=ctk.ThemeManager.theme["CTkButton"]["corner_radius"] do, these arguments are changing the label color and corner radius to match what a button looks like.

    I guess you could do even better using a class to create a personalized button but this seems good enough.

    Hope I helped you, have a nice day.