pythonpython-3.xuser-interfacetkintercheckbox

Tkinter Checkbuttons not changing the variable value


My skills with Tkinter are improving day by day, I cannot believe how far I am compared to 2 weeks ago.

Now my problem is that I cannot make the Checkbuttons work. For some reason that escapes my mind, they always remain with the same value, it doesn't matter if they are checked or not.

I have tried the checkbuttons alone in another script and they work perfectly. They also work in another windows from my GUI, but when I put them on the window they should be in, they just stop working.

Do you have any idea why this could be happening?

Thank you! :D

I tried setting different default values to the check button, like 10 for active and 20 for not active, but the variable does not change, it still has a value of 0.

from tkinter import *


def runp(): 

    def cb(vari):
        print ("variable is {0}".format(vari.get()))


    window = Tk()
    window.title("Please choose the parameters")
    window.geometry('500x350')

    labelSelect=Label(window, text="Which Rdata file would you like to load? (from output directory)")
    labelSelect.grid(column=0, row=11)

    FastaC=BooleanVar()
    RwMatrix=BooleanVar()
    RwSum=BooleanVar()
    RwInfo=BooleanVar()

    FastaCRadio=Checkbutton(window, text="FastaClean.Rdata", variable=FastaC, command=lambda: cb(FastaC))
    FastaCRadio.grid(column=1, row=11)

    RwMatrixRadio=Checkbutton(window, text="RwMatrix.Rdata", variable=RwMatrix, command=lambda: cb(RwInfo))
    RwMatrixRadio.grid(column=1, row=12)
    RwSumRadio=Checkbutton(window, text="RwSum.Rdata", variable=RwSum, command=lambda: cb(RwSum))
    RwSumRadio.grid(column=1, row=13)
    RwInfoRadio=Checkbutton(window, text="RwInfo.Rdata", variable=RwInfo,command=lambda:cb(RwInfo))
    RwInfoRadio.grid(column=1, row=14)

    window.mainloop()

master=Tk()

Button(master, text="RW", command=runp).pack()

master.mainloop()


Solution

  • You need to create child window by creating instances of Toplevel:

    change window= Tk() to window= Toplevel()

    need to use Toplevel() for a window that opens on another window.

    code:

    from tkinter import *
    
    
    def runp():
    
        def cb(vari):
            print ("variable is {0}".format(vari.get()))
    
    
        window = Toplevel() # <-------------------
        window.title("Please choose the parameters")
        window.geometry('500x350')
    
        labelSelect=Label(window, text="Which Rdata file would you like to load? (from output directory)")
        labelSelect.grid(column=0, row=11)
    
        FastaC=BooleanVar()
        RwMatrix=BooleanVar()
        RwSum=BooleanVar()
        RwInfo=BooleanVar()
    
        FastaCRadio=Checkbutton(window, text="FastaClean.Rdata", variable=FastaC, command=lambda: cb(FastaC))
        FastaCRadio.grid(column=1, row=11)
    
        RwSumRadio=Checkbutton(window, text="RwMatrix.Rdata", variable=RwMatrix, command=lambda: cb(RwMatrix))
        RwSumRadio.grid(column=1, row=12)
        RwSumRadio=Checkbutton(window, text="RwSum.Rdata", variable=RwSum, command=lambda: cb(RwSum))
        RwSumRadio.grid(column=1, row=13)
        RwInfoRadio=Checkbutton(window, text="RwInfo.Rdata", variable=RwInfo,command=lambda:cb(RwInfo))
        RwInfoRadio.grid(column=1, row=14)
    
        window.mainloop()
    
    master=Tk()
    
    Button(master, text="RW", command=runp).pack()
    
    master.mainloop()