0

I am trying to get the following code to work, could any point out where I am going wrong?

The object is to set the menu state to disabled when a new form is loaded and then to set the menu state back to normal once the form is closed. The purpose of doing this is to only have one form (menu item) open at a time.

Any suggestions on how to achieve this would be appreciated.

Menutest

from tkinter import *

import BackupData

window = Tk()

Window_X = 600
Window_Y = 700
Screen_X = window.winfo_screenwidth()
Screen_Y = window.winfo_screenheight()
ScnDisp_X = int ((Screen_X/2) - (Window_X/2))
ScnDisp_Y = int ((Screen_Y/2) - (Window_Y/2))

window.geometry("%dx%d+%d+%d" % (Window_X,Window_Y,ScnDisp_X,ScnDisp_Y, ))
window.configure(bg="#EDF9EB") #green background

def BackupFun():
    #MenuDisable()
    menubar.entryconfig(1, state = DISABLED)
    BackupData.BackupMod(window)
    menubar.entryconfig(1, state = NORMAL)

menubar = Menu(window, bg="#333333")
window.config(menu=menubar)

# Admin Menu
ADM = Menu(menubar, tearoff=0, activebackground ="#333333", bg= "#BCC6CC")
menubar.add_cascade(label="Admin", menu=ADM )
ADM.add_command(label="Backup Data", command=BackupFun, state="normal")
ADM.add_separator()
ADM.add_command(label="Check for updates", command=None, state="disabled")
ADM.add_separator()
ADM.add_command(label="Exit", command=window.destroy)

window.mainloop()type here

Backup module

from tkinter import *

def BackupMod(window):
    global fr0
    FrmWidth = 200 
    FrmHeight = 100
    fr0 = Frame(window, borderwidth=2, width=FrmWidth, height=FrmHeight, relief="ridge" )
    fr0.grid(padx=20, pady=30)
    
    button = Button(fr0, text="Close", command= Closefr0, width=10)
    button.grid (row=1, column=2, sticky="news", padx=20, pady=20)    

def Closefr0():
    fr0.destroy()

your text

1 Answer 1

0

The reason why it isn't working is because you are disabling the menubar creating the form and then re-enabling it before the gui has a chance to even render the form.

Once you call the BackupMod function inside of the BackupFun, your code continues to execute the rest of the function and then exits, so to the user it's like nothing ever happened with the menubar.

There are a few ways to solve this, but the one that requires maybe the fewest changes would be to remove the line that re-enables the menubar, add the menubar as an argument to the BackupMod function and then pass the menubar to the closeFr0 function callback and have that callback function trigger the re-enable.

For example:

from tkinter import *

def BackupMod(window, menubar):
    global fr0
    FrmWidth = 200 
    FrmHeight = 100
    fr0 = Frame(window, borderwidth=2, width=FrmWidth, height=FrmHeight, relief="ridge" )
    fr0.grid(padx=20, pady=30)
    button = Button(fr0, text="Close", command= lambda: Closefr0(menubar), width=10)
    button.grid (row=1, column=2, sticky="news", padx=20, pady=20)    


def Closefr0(menubar):
    fr0.destroy()
    menubar.entryconfig(1, state = NORMAL)
from tkinter import *
import BackupData

window = Tk()

Window_X = 600
Window_Y = 700
Screen_X = window.winfo_screenwidth()
Screen_Y = window.winfo_screenheight()
ScnDisp_X = int ((Screen_X/2) - (Window_X/2))
ScnDisp_Y = int ((Screen_Y/2) - (Window_Y/2))

window.geometry("%dx%d+%d+%d" % (Window_X,Window_Y,ScnDisp_X,ScnDisp_Y, ))
window.configure(bg="#EDF9EB") #green background

def BackupFun():
    #MenuDisable()
    menubar.entryconfig(1, state = DISABLED)
    BackupData.BackupMod(window, menubar)

menubar = Menu(window, bg="#333333")
window.configure(menu=menubar)

# Admin Menu
ADM = Menu(menubar, tearoff=0, activebackground ="#333333", bg= "#BCC6CC")
menubar.add_cascade(label="Admin", menu=ADM )
ADM.add_command(label="Backup Data", command=BackupFun, state="normal")
ADM.add_separator()
ADM.add_command(label="Check for updates", command=None, state="disabled")
ADM.add_separator()
ADM.add_command(label="Exit", command=window.destroy)

window.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.