mercredi 24 mai 2017

How to disable a checkbutton(tkinter) after it has been clicked once

In short, I am creating a text based game with Tkinter. In the game, the user is offered a package and is asked if they would like to receive it. This checkbutton is created in a function and is only created if the user enters the correct name. I would like this button to become DISABLED after it has been checked once. This is what my code looks like:

def check_name(name):
    if "cole" in name:
        receive_is_checked = IntVar()
        receive_pkg = Checkbutton(main, text="Receive the package?", command=MailWindow,
                                  onvalue=0, offvalue=1, variable=receive_is_checked)
        receive_pkg.pack()
        if receive_is_checked.get():
            receive_pkg.config(state=DISABLED)

I am aware that the common answer to this problem is to 'make a function that can disable the checkbutton,' or because I already am using the command option, 'make a function that does both functions' but my issue is is that I'm using command to make a class object which will open up another window and cannot alter the checkbutton in this function. I don't want the user to be able to open up multiple of the pop up windows. I've tried doing this same thing with a button and ran into the same issues.

If anyone could help me figure out why I cant disable the button after it is checked or help me find an alternative that would be much appreciated (note: I have also tried the same thing but just destroying the checkbutton after it has been checked and this does not work either).




Aucun commentaire:

Enregistrer un commentaire