Jimothy Cardotha
Jimothy Cardotha

Reputation: 103

How to change the color of a tk.Menu object?

So I made this text editor with tkinter and python. It has a menu bar with a File drop down menu.

Here's a screen shot: enter image description here

I've been trying to change the color of it but no matter what I do nothing works.

Here's a code snippet:

class MenuBar:
    def __init__(self, mainClass):
        fontSpecs = ("ubuntu", 9)

        menuBar = tk.Menu(mainClass.win, font=fontSpecs)
        mainClass.win.config(bg="#3C3F41", menu=menuBar)

        fileDropDown = tk.Menu(menuBar, font=fontSpecs, tearoff=0, fg="#AFB1B3", bg="#313335")
        fileDropDown.add_command(label="New File", command=mainClass.NewFile, accelerator="Ctrl+N")
        fileDropDown.add_command(label="Open File", command=mainClass.OpenFile, accelerator="Ctrl+O")
        fileDropDown.add_command(label="Save", command=mainClass.Save, accelerator="Ctrl+S")
        fileDropDown.add_command(label="Save As", command=mainClass.SaveAs, accelerator="Ctrl+Shift+S")
        fileDropDown.add_separator()
        fileDropDown.add_command(label="Exit", command=mainClass.Exit)

        menuBar.add_cascade(label="File", menu=fileDropDown)

Here's the __init__() method of the main class:

class TextEditor:
    def __init__(self, win: tk.Tk):
        win.title(f"Untitled - {name}")
        win.geometry("1100x600")

        fontSpecs = ("ubuntu", 12)

        self.win = win
        self.fileName = None

        self.textArea = tk.Text(self.win, font=fontSpecs, insertbackground="#AFB1B3", fg="#AFB1B3", bg="#313335")
        self.scroll = tk.Scrollbar(self.win, command=self.textArea.yview())
        self.textArea.configure(yscrollcommand=self.scroll.set)
        self.textArea.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        font = tk_font.Font(font=self.textArea["font"])
        tab = font.measure("    ")  # 4 empty spaces
        self.textArea.config(tabs=tab)

        self.scroll.pack(side=tk.RIGHT, fill=tk.Y)

        self.menuBar = MenuBar(self)

If you need any more info, please ask.

Upvotes: 2

Views: 257

Answers (2)

Saad
Saad

Reputation: 3430

You can create something similar from Tkinter widgets like Frame and Menu. It is very simple we can bind post(x, y) method of the menu with a label of a button to get a menu at any (x, y) coordinate but here we will use x=root x coordinate of the label and y=root x coordinate of the label + height of the label.

As I'm on macOS and as I don't have access to a windows machine so I could not test it on the window but I'm sure it should work fine on windows.

Here is the sample and not a complete version. This will get you started and you can modify or add features according to your need.

import tkinter as tk


class CustomMenuBar(tk.Frame):
    def __init__(self, master=None, cnf={}, **kw):
        kw = tk._cnfmerge((cnf, kw))
        kw['relief'] = kw.get('relief', 'raised')
        self._fg = kw.pop('fg', kw.pop('foreground', 'black'))
        self._over_bg = kw.pop('overbackground', 'blue')
        super().__init__(master=master, **kw)
        self._lb_list = []
    
    def _on_press(self, label, command=None):
        """Internal function.\n
        This is called when a user clicks on a menubar."""
        label.menu.post(label.winfo_rootx(), 
            label.winfo_rooty() + label.winfo_height() + 5) # 5 padding (set accordingly)
        if command: command()  # Calls the function passed to `add_menu` method.
    
    def add_menu(self, title, menu, command=None):
        """Add menu labels."""
        l = tk.Label(self, text=title, fg=self._fg, bg=self['bg'], padx=2, pady=2)
        l.pack(side='left')
        l.bind('<Enter>', lambda e: l.config(bg=self._over_bg))
        l.bind('<Leave>', lambda e: l.config(bg=self['bg']))
        l.menu = menu  # Easy to access menu with the instance 
                       #   of the label saved in the `self._lb_list`
        l.bind('<1>', lambda e: self._on_press(l, command))
        self._lb_list.append(l)


def demo():
    root = tk.Tk()
    root.geometry('200x200')
    mb = CustomMenuBar(root, bg='black', fg='white', overbackground='#2C41FF')
    mb.pack(side='top', expand=1, fill='x', anchor='n')

    filemenu = tk.Menu(mb)
    filemenu.add_command(label='New')
    filemenu.add_command(label='Open')
    filemenu.add_separator()
    filemenu.add_command(label='Exit', command=root.quit)

    editmenu = tk.Menu(mb)
    editmenu.add_command(label='Copy')
    editmenu.add_command(label='Paste')

    mb.add_menu('File', filemenu)
    mb.add_menu('Edit', editmenu)

    root.mainloop()

if __name__ == "__main__":
    demo()   

Upvotes: 2

Maimas2
Maimas2

Reputation: 961

You cannot change the color of a tk.Menu object.

Upvotes: 2

Related Questions