0

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

Bektas
  • 3
  • 2
  • It works in my Windows 7 with Python 3.8.16. However what is the point of setting `relief` option when `bd=0`? – acw1668 Mar 01 '23 at 09:36
  • @acw1668 ChatGPT created this code for me, I'm not very familiar with tkinter. – Bektas Mar 01 '23 at 09:45

2 Answers2

1

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.

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
0
import tkinter as tk

window = tk.Tk()
window.title("Hello world")
window.geometry("300x300")

bg_color = (0x31, 0x8C, 0xE7)
bg_hex = '#{:02x}{:02x}{:02x}'.format(*bg_color)

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

window.mainloop()

I tested it in repl.it and it worked.