weilueluo
weilueluo

Reputation: 683

How to get user current system theme color

hi I am working on a python gui project and I find that the color of the frame of my application depends on the user's system theme color. I want my application to have the same color, may I know how can I achieve that? My application is mainly for windows but if theres a way for linux that would be great. I am using tkinter btw.
Or maybe how to get the color from commandline since we have os.system(command) ?

Update: I have tried inputs listed in widget styling but none of them work, my system has a theme color of light blue

from tkinter import *

if __name__ == '__main__':
    root = Tk()
    root['bg'] = 'SystemButtonFace'  # white
    root['bg'] = 'SystemBackground'  # black
    root['bg'] = 'SystemButtonText'  # black
    root['bg'] = 'SystemAppWorkspace'  # grey
    root['bg'] = 'SystemActiveBorder'  # grey
    root['bg'] = 'SystemActiveCaption'  # light grey
    root['bg'] = 'SystemInactiveCaption'  # light grey
    root['bg'] = 'SystemButtonShadow'  # darker grey
    root['bg'] = 'SystemButtonHighlight'  # white with a bit grey
    root['bg'] = 'SystemCaptionText'  # black
    root['bg'] = 'SystemDisabledText'  # darker grey
    root['bg'] = 'SystemHighlight'  # light dark blue
    root['bg'] = 'SystemHighlightText'  # white
    root['bg'] = 'SystemInactiveBorder'  # white
    root['bg'] = 'SystemInactiveCaptionText'  # black
    root['bg'] = 'SystemMenu'  # white
    root['bg'] = 'SystemMenuText'  # black
    root['bg'] = 'SystemScrollbar'  # grey
    root['bg'] = 'SystemWindow'  # white
    root['bg'] = 'SystemWindowFrame'  # dark grey
    root['bg'] = 'SystemWindowText'  # black

    root.mainloop()

Setting the background to black does not set the frame to black and I can't find a way to get the light blue color
info

Upvotes: 2

Views: 2885

Answers (3)

Valer
Valer

Reputation: 73

You can do that using winaccent, a Python module I made for retrieving the accent color, a shade of it, active/inactive titlebar color and window borders color.

You can install it using the following command:

pip install winaccent

Then, you can change your tkinter app's background color like this:

import tkinter, winaccent

root = tkinter.Tk()

# Check if the titlebar is colored
if winaccent.is_titlebar_colored:
    root.configure(bg = winaccent.titlebar_active)
else:
    root.configure(bg = "SystemButtonFace")

root.mainloop()

If "Show accent color on title bars and window borders" option is enabled in Settings, your window will look like this.

If not, it will look like this.

If you want to see more features of this module, you can read the documentation here.

Upvotes: 0

JimShapedCoding
JimShapedCoding

Reputation: 917

You have to use a customized RGB color by yourself.

Use the key 'bg' to know your background color.

Anyway, if you want to achieve own customizing of a Tkinter frame, you can do the following:

from tkinter import *

root = Tk()

print(root['bg']) # Output: SystemButtonFace
root.configure(bg = '#FF0000') # Configuring new color RED
print(root['bg']) # Output: #FF0000

root.mainloop()

There is more about the tkinter styling on this page:

Widget Styling

Look at the paragraph of the Default system colors for Macintosh and Windows.

There are chances that there are no system colors for Linux as it is managed usually by a black command line.

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 386220

Tkinter cannot change the color of the window border, nor get the color. You will need to use some other library specific to your window manager to get or change the color.

Upvotes: 1

Related Questions