I am using tkinter simpledialog.askinteger in Python and it is working beautifully, but I would like to change the command associated with the "Cancel" button. I have looked at the Python documentation on this command, found here: https://docs.python.org/3/library/dialog.html However, all it says is that you can override the button commands; it does not provide instruction on how one would go about doing that. I am rather new to Python, so I am afraid I need it spelled out a little more explicitly.
The relevant code, when simplified, looks something like this:
from tkinter import simpledialog
from tkinter import messagebox
prioritychoice = True
while prioritychoice == True:
P1Cont = simpledialog.askinteger('Priority Level','Which priority level would you like to view?')
# the code then displays a dataframe containing the correct priority info
prioritychoice = messagebox.askyesno('Continue', 'Would you like to see information for a different priority level?')
[This code produces a dialog box that looks like this] (https://i.sstatic.net/TMjsw63J.png)
I would really like to override the command for the 'Cancel' button so that it breaks out of the while loop instead of just closing the window.
Is this possible? I see a lot of answers on Stack talking about buttons in a custom tkinter popup, but not specifically within the simpledialog.
You don't need to override the Cancel command. Simply check whether P1Cont
is None, if yes then break the while loop.
prioritychoice = True
while prioritychoice == True:
P1Cont = simpledialog.askinteger(
'Priority Level',
'Which priority level would you like to view?'
)
if P1Cont is None:
# Cancel button clicked, break the while loop
break
# the code then displays a dataframe containing the correct priority info
prioritychoice = messagebox.askyesno(
'Continue',
'Would you like to see information for a different priority level?'
)