Jump to content

Python Tkinter menu colours

Go to solution Solved by reniat,
5 hours ago, mattl1598 said:

sorry to ask again but I can't work out which option the border is controlled by.

image.png.2fd1e71ae4cf2bb2c351880df0a4516b.png

 

it doesn't get removed by setting the border width to 0 and i've tried all of the colour things that i can change for the optionmenu["menu"] object.

 

try this: https://stackoverflow.com/questions/39921360/how-to-remove-the-border-of-a-tkinter-optionmenu-widget
also, both of these are from the first page of a google search ;) Don't hesitate to let yourself dig through search results once and a while. It's a good skill to have.

I'm trying to make a settings menu for my A-Level coursework in Computer Science and one of my aims is to have a themes system and a menu to change it. 

 

So far, the theme switching menu works and changes the backgrounds, text colours, button colours etc but the actual drop down menu doesn't change colour according to the theme and it doesn't look very good on the dark theme background.

 

Has anyone got much experience with tkinter and can tell me which config value I need to set to change the colour?

 

(attached are an image of my settings menu with the drop down open and the code for getting colours from json file and creating the option menu)

 

Code:

theme import from json:

Spoiler



	def gettheme(self):

		#get colours from json file
		with open("D:/Project-Bookworm/theme.json", "r") as readfile:
			theme1 = json.load(readfile)

		theme = theme1["theme"]

		bg = theme1[theme]["windows"]["background"]
		text = theme1[theme]["windows"]["text"]
		buttonBG = theme1[theme]["button"]["background"]
		buttTXT = theme1[theme]["button"]["text"]
		boxBG = theme1[theme]["textbox"]["background"]
		boxTXT = theme1[theme]["textbox"]["foreground"]
		cursor = theme1[theme]["textbox"]["insertbackground"]
		select = theme1[theme]["textbox"]["selectbackground"]
		clickedbg = theme1[theme]["button"]["clickedbg"]

		return bg, text, buttonBG, buttTXT, boxBG, boxTXT, cursor, select, clickedbg, theme


 

creating optionmenu:

Spoiler



	def __init__(self):


		#import colours
		bg, text, buttonBG, buttTXT, boxBG, boxTXT, cursor, select, clickedbg, currentTheme = self.gettheme()
		self.setts = tkinter.Tk()
		self.setts.title("Settings")
		self.setts.configure(background=bg)

		themes = [
			"dark",
			"light",
			"custom"
		]

		self.theme = tkinter.StringVar(self.setts)
		self.theme.set(currentTheme)


		#option menu setup and config
		self.themeDrop = tkinter.OptionMenu(self.setts, self.theme, *themes)
		self.themeDrop.configure(background=buttonBG, foreground=buttTXT, activebackground=clickedbg, activeforeground=buttTXT, highlightthickness=0, highlightcolor=bg, highlightbackground=bg, disabledForeground=bg)
		#print(self.themeDrop.config())
		self.themeDrop.pack()


 

json file:

Spoiler



{
    "theme": "dark",
    "dark": {
        "windows": {
            "background": "#474747",
            "darkbackground": "#2f2f2f",
            "highlightbackground": "#474747",
            "highlightcolor": "black",
            "text": "white"
        },
        "textbox": {
            "background": "#2f2f2f",
            "foreground": "white",
            "highlightbackground": "#d9d9d9",
            "highlightcolor": "black",
            "insertbackground": "#c4c4c4",
            "selectbackground": "#214283",
            "selectforeground": "black"
        },
        "button": {
            "background": "#474747",
            "text": "white",
            "clickedbg": "#2f2f2f"
        }
    },
    "light": {
        "windows": {
            "background": "#d9d9d9",
            "darkbackground": "#d9d9d9",
            "highlightbackground": "#d9d9d9",
            "highlightcolor": "black",
            "text": "black"
        },
        "textbox": {
            "background": "white",
            "foreground": "black",
            "highlightbackground": "#d9d9d9",
            "highlightcolor": "black",
            "insertbackground": "#c4c4c4",
            "selectbackground": "black",
            "selectforeground": "black"
        },
        "button": {
            "background": "#d9d9d9",
            "text": "black",
            "clickedbg": "#d9d9d9"
        }
    },
    "custom": {
        "windows": {
            "background": "SystemButtonFace",
            "darkbackground": "#2f2f2f",
            "highlightbackground": "#474747",
            "highlightcolor": "black",
            "text": "white"
        },
        "textbox": {
            "background": "#2f2f2f",
            "foreground": "white",
            "highlightbackground": "#d9d9d9",
            "highlightcolor": "black",
            "insertbackground": "#c4c4c4",
            "selectbackground": "#214283",
            "selectforeground": "black"
        },
        "button": {
            "background": "#474747",
            "text": "white",
            "clickedbg": "#2f2f2f"
        }
    }
}


 

 

settings2.PNG

SPECS:

AMD Ryzen 1800x @4Ghz

32GB DDR4 G.Skill Trident-Z RGB 3200Mhz

ASUS ROG STRIX B350-F Gaming motherboard

RX 5700 XT

750W Corsair PSU

Fractal Design Meshify C

250gb WD Black NVMe SSD

 

Peripherals:

Asus VG24V 144hz 1080p

Logitech MX Master

Corsair K70 Mk2

Stream Deck

GoXLR Mini

Logitech Z330, Corsair Virtuoso

Link to comment
Share on other sites

Link to post
Share on other sites

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

Do you call the init function every time you change theme? If not, the reason this doesn't work is that the menu is created with the default theme and never updated.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Sauron said:

Do you call the init function every time you change theme? If not, the reason this doesn't work is that the menu is created with the theme and never updated.

sort of. when i change theme the close button in the settings menu functions as an apply and close. so it goes back to the main menu which gets re-opened from it's init function and you go back into settings from there.

SPECS:

AMD Ryzen 1800x @4Ghz

32GB DDR4 G.Skill Trident-Z RGB 3200Mhz

ASUS ROG STRIX B350-F Gaming motherboard

RX 5700 XT

750W Corsair PSU

Fractal Design Meshify C

250gb WD Black NVMe SSD

 

Peripherals:

Asus VG24V 144hz 1080p

Logitech MX Master

Corsair K70 Mk2

Stream Deck

GoXLR Mini

Logitech Z330, Corsair Virtuoso

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, reniat said:

thank you this is working now. i should be able to get rid of the weird border issues from here.

image.png.5264dd3c9e1afc907e60a4949835d62f.png

 

thanks again

SPECS:

AMD Ryzen 1800x @4Ghz

32GB DDR4 G.Skill Trident-Z RGB 3200Mhz

ASUS ROG STRIX B350-F Gaming motherboard

RX 5700 XT

750W Corsair PSU

Fractal Design Meshify C

250gb WD Black NVMe SSD

 

Peripherals:

Asus VG24V 144hz 1080p

Logitech MX Master

Corsair K70 Mk2

Stream Deck

GoXLR Mini

Logitech Z330, Corsair Virtuoso

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, mattl1598 said:

sort of. when i change theme the close button in the settings menu functions as an apply and close. so it goes back to the main menu which gets re-opened from it's init function and you go back into settings from there.

Try manually changing the theme after creating the menu, that should tell us if the problem is caused by only applying it on creation. Also, have you read this?

 

-edit-

well looks like I'm late to the party ^^

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

18 hours ago, reniat said:

sorry to ask again but I can't work out which option the border is controlled by.

image.png.2fd1e71ae4cf2bb2c351880df0a4516b.png

 

it doesn't get removed by setting the border width to 0 and i've tried all of the colour things that i can change for the optionmenu["menu"] object.

 

SPECS:

AMD Ryzen 1800x @4Ghz

32GB DDR4 G.Skill Trident-Z RGB 3200Mhz

ASUS ROG STRIX B350-F Gaming motherboard

RX 5700 XT

750W Corsair PSU

Fractal Design Meshify C

250gb WD Black NVMe SSD

 

Peripherals:

Asus VG24V 144hz 1080p

Logitech MX Master

Corsair K70 Mk2

Stream Deck

GoXLR Mini

Logitech Z330, Corsair Virtuoso

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, mattl1598 said:

sorry to ask again but I can't work out which option the border is controlled by.

image.png.2fd1e71ae4cf2bb2c351880df0a4516b.png

 

it doesn't get removed by setting the border width to 0 and i've tried all of the colour things that i can change for the optionmenu["menu"] object.

 

try this: https://stackoverflow.com/questions/39921360/how-to-remove-the-border-of-a-tkinter-optionmenu-widget
also, both of these are from the first page of a google search ;) Don't hesitate to let yourself dig through search results once and a while. It's a good skill to have.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 years later...

I apologize for reviving this thread, but did you happen to figure out how to change the color of the border surrounding the options (the white border in your dark theme screenshot)? I am able to change the background of the options and their font color with the following:

dropdownmenu["menu"].config(bg="grey", fg="white")

However, I can not seem to figure out how to change the ugly border surrounding the options.

Link to comment
Share on other sites

Link to post
Share on other sites

On 5/7/2021 at 1:39 AM, Voltaia said:

However, I can not seem to figure out how to change the ugly border surrounding the options.

Hm.. why do you even want to recolor the UI. You either should aim for native look or in some cases - for completely custom look that doesn't resemble native UI elements. On top of that tkinter is pretty much obsolete in terms of UX/UI -  you won't get a good app out of that.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×