0

I'm trying to create a class that allows you to have a MenuBar that can be personalized in windows. It's nothing special since I'm new to coding. I'm trying different ways to change the background of the label behind the cascade buttons but I can only change the buttons ones.

from tkinter import *
TESTING = True
root = Tk()
root.attributes('-fullscreen',True)
class menubar():
    """You can use this to create a menu bar
    There are some functions:
    ButtonCascadeAdd: Lets you create a cascade button to which you will be able to add more buttons with 
    AddButtons function
    AddButtons: just adds buttons to the cascades. you can chose cascades changing the input number from 
    0 to 9
    Soon will be added color switch and others althought you can already do those things by yourself 
    since every btns[] object is a tkinter.Menubutton()"""
    global labelframe
    global contx
    global btns
    contx = 0
    labelframe = LabelFrame(root)
    btns = [Menubutton(labelframe) for i in range(10)]

    def place(self, width = 300,height = 30,rely=0):
        labelframe.place(width=width, height=height, rely=rely)

    def ButtonCascadeAdd(self,text = "btns[",tearoff = 0):
        """Adds a cascade button.
        You can pass in text to change the button title default is the name of the button in the code
        so maybe it will prove useful to leave it like that for develop purpose.
        You cant add a command to that from here.
        If you need a  button with a command and not a cascade look aat the FunctionButtonAdd function"""
        global contx
        if text == "btns[":
            text = text+str(contx)+"]"
        if contx == 10:
            print("Max number of buttons exceeded the program will be stopped")
            exit()
        b = btns[contx]
        b.config(text = text)
        b.pack(side=LEFT)
        b.menu = Menu(b, tearoff=tearoff)
        b["menu"] = b.menu
        labelframe.update()
        contx += 1
        print(contx)

    def maincolor(self,bg = "light green",fg = "black",activebg = "green",activefg = "dark blue"):
        global contx
        for x in range(contx):
            btns[x].config(bg=bg, fg=fg, activebackground=activebg, activeforeground=activefg)
            btns[x].menu.config(bg = "black",fg = "white")

        labelframe.config(bg=bg)

def doNothing():
    print("Doing Nothing ^^")
if TESTING ==   True:
    m = menubar()
    m.place(width = 1980,height = 20)
    m.ButtonCascadeAdd()
    m.ButtonCascadeAdd()
    btns[0].menu.add_command(label="test")
    #print(dir(btns[0].menu))
    m.maincolor()
    root.mainloop()
Anshika Singh
  • 994
  • 12
  • 20
  • Did you try to call `m.maincolor()` again with different colors? – acw1668 Aug 06 '20 at 12:43
  • Actually no but al the colors changed by that function are not an issue. The problem is I can’t change the gray color of the auto generated label in the cascade of button behind the buttons. Hope you can understand of which part I was talking about – Riccardo Tolusso Aug 06 '20 at 21:09
  • Do you mean the gray border mentioned in this question: [is-it-possible-to-change-the-menu-border-color-in-tkinter](https://stackoverflow.com/questions/58678381/is-it-possible-to-change-the-menu-border-color-in-tkinter)? – acw1668 Aug 07 '20 at 05:17

0 Answers0