0

I am having trouble triggering some if-else condition with global variables when using the Toplevel window Buttons in tkinter.

Here is the relevant snippet of code:

from tkinter import ttk
from tkinter import *

DELETE_ENTRY_STATUS = bool
DELETE_ENTRY_NUM = 0

def messagebox():                                               
    global DELETE_ENTRY_STATUS, DELETE_ENTRY_NUM

        top = Toplevel(bg = 'red')
        top.geometry('210x100')
        top.title('A.R.A.H')
        delete_confirmation_btn = Button(top, text='Confirm',width=12,bg='brown',fg='white',command=deleteconfirmbit).pack(pady = 20)
        delete_cancel_btn = Button(top, text='Cancel',width=12,bg='brown',fg='white',command=deletecancelbit).pack()

        ```This is where I am unable to receive the updated DELETE_ENTRY_NUM from deleteconfirmbit & deletecancelbit```

        if DELETE_ENTRY_NUM == 1:
            top.after(0, top.destroy)  

def delete_all_option():
    global DELETE_ENTRY_STATUS, DELETE_ENTRY_NUM

    messagebox()
    
    if DELETE_ENTRY_STATUS:
        ```Doing some Treeview Operation```

def deleteconfirmbit():
    global DELETE_ENTRY_STATUS, DELETE_ENTRY_NUM

    DELETE_ENTRY_STATUS = True
    DELETE_ENTRY_NUM = 1
    
       
def deletecancelbit():
    global DELETE_ENTRY_STATUS, DELETE_ENTRY_NUM

    DELETE_ENTRY_STATUS = False
    DELETE_ENTRY_NUM = 1


```Some main codes```

delete_all_option()

```Some more main codes```

I understand this is likely due to the way the function was called from this thread but I am unable to figure out how I should re-order the function call to get the DELETE_ENTRY_NUM to be updated after deleteconfirmbit & deletecancelbit function calls. Thanks in advance.

Justin
  • 87
  • 1
  • 6
  • The key is that `messagebox` just needs to display the dialog and exit. The actions all get taken in the two button handlers, which need to do the operation and hide the dialog. However, there is already a `tkinter.messgebox` that can do exactly what you're doing here, and it returns a value so you can handle things in `delete_all_option`.' – Tim Roberts May 28 '21 at 03:18
  • Hi @TimRoberts, right.. I didn't think of it that way, it works for now but doesn't do exactly what I would help cause I am still figuring it out. But thanks a lot for the suggestion, will look into `tkinter.messgebox` too. Should I simply delete this post or perhaps I can leave your suggestion as an answer? – Justin May 28 '21 at 03:32

0 Answers0