Search code examples
pythontkintertkinter-button

Can't customize TKInter button


I want to make the bg of the button '#318CE7' this hex color, but no matter what I tried, the color did not change and when the application ran, the color of the button became white.

bg_color = (49, 140, 231)
bg_hex = '#{:02x}{:02x}{:02x}'.format(*bg_color)

start_btn = tk.Button(root, text="Start", bg=bg_hex, fg="white", font=("Arial", 16), bd=0, relief=tk.RIDGE, command=start_app)

enter image description here

I tried solving the problem with ChatGPT but the button did not turn blue.

I'm using macOS Big Sur


Solution

  • This is a common issue for tkinter and mac sadly. I have run into similar things when i was using tkinter for a small app. The solution to this is to install the dedicated mac library. A sample code is:

    import tkinter as tk
    from tkmacosx import Button
        
    bg_color = (0x31, 0x8C, 0xE7)
    bg_hex = '#{:02x}{:02x}{:02x}'.format(*bg_color)
    
    start_btn = Button(root, text="Start", bg=bg_hex, fg="white", font=("Arial", 16), bd=0, relief=tk.RIDGE, command=start_app)
    

    As you can see we are using the Button from the tkmacosx package, that will solve your issue and make the button blue.

    Just install the tkmacosx before you run this with pip.