简体   繁体   中英

Changing the colour of tkinter menubar

I have the following code, what I'm trying to do is change the color the the menubar to be the same as my window. I have tried what you see below, adding to bg="#20232A" to menubar but this seems to have no affect..

My Question: The below image is the window (albeit a snippet of the window), it showcases both the menubar and background. I want the menubar to be the same color as the background seen below, how can I achieve this?

在此处输入图像描述

from tkinter import *

config = {"title":"Editor", "version":"[Version: 0.1]"}

window = Tk()
window.title(config["title"] + " " +config["version"])
window.config(bg="#20232A")
window.state('zoomed')

def Start():
    menubar = Menu(window, borderwidth=0, bg="#20232A") # Tried adding background to this, but it doesent work

    filemenu = Menu(menubar, tearoff=0)
    filemenu.add_command(label="Open")
    filemenu.add_command(label="Save")
    menubar.add_cascade(label="File", menu=filemenu)
    window.config(menu=menubar)

Start()
window.mainloop()

You cannot change the color of the menubar on Windows or OSX. It might be possible on some window managers on linux, though I don't know for certain.

The reason is that the menubar is drawn using native widgets that aren't managed by tkinter, so you're limited to what the platform allows.

On Linux it is possible:

def main():

    root =Tk()
    menubar = Menu(root, background='lightblue', foreground='black',
               activebackground='#004c99', activeforeground='white')  
    file = Menu(menubar, tearoff=1, background='lightblue', foreground='black')  
    file.add_command(label="Receive")  
    file.add_command(label="Issue")  
    file.add_command(label="Track")  
    file.add_command(label="Search")  
    file.add_command(label="Allocate")  
    
    file.add_separator()  
    
    file.add_command(label="Exit", command=root.quit)  
    
    menubar.add_cascade(label="Goods", menu=file)  
    edit = Menu(menubar, tearoff=0)  
    edit.add_command(label="Undo")  
    
    edit.add_separator()  
    
    edit.add_command(label="Cut")  
    edit.add_command(label="Copy")  
    edit.add_command(label="Paste")  
    edit.add_command(label="Delete")  
    edit.add_command(label="Select All")  
    
    menubar.add_cascade(label="Accounts", menu=edit)  
    help = Menu(menubar, tearoff=0)  
    help.add_command(label="About")  
    menubar.add_cascade(label="Help", menu=help)  
    
    root.config(menu=menubar)  
    ex = MainWin()
    root.geometry("2000x1391")
    root.mainloop()


if __name__ == '__main__':
    main()

Just add foreground and background attributes.

To follow on from Brian Oakley's response. The menu bar on Linux (at least on my Linux Mint installation) renders the desired color.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM