mardi 12 novembre 2019

Checkbox variable not updating in tkinter

I am a bit new to Tkinter. I'm running Python 3.7.4 on Windows x64. I am trying to make a simple checkbox driven menu that will pass certain values based on the checked boxes. Here is the code:

def updategui():
    winu = tkinter.Tk()
    winu.geometry("500x200")
    winu.title("Update")
    caution = tkinter.Label(winu, text="Please select the option if you are updating the corresponding value")
    caution.grid(row=0, column=0)
    sitel = tkinter.Label(winu, text="Enter site name: ")
    sitee = tkinter.Entry(winu, width=20)
    userv = tkinter.IntVar()
    userc = tkinter.Checkbutton(winu, text="New Username: ", variable=userv)
    passv = tkinter.IntVar()
    passc = tkinter.Checkbutton(winu, text="New Password: ", variable=passv)
    usere = tkinter.Entry(winu, width=20)
    passe = tkinter.Entry(winu, width=20)
    sitel.grid(row=1, column=0)
    sitee.grid(row=1, column=1)
    userr.grid(row=2, column=0)
    usere.grid(row=2, column=1)
    passr.grid(row=3, column=0)
    passe.grid(row=3, column=1)

    def updatebt():
        userval = int(userv.get())
        passval = int(passv.get())
        print(userval, passval)
        site = str(sitee.get())
        if userval == 1 and passval == 0:
            user = str(usere.get())
            passw = None
            main.update(site, user, passw)
            messagebox.showinfo("Success", "Username updated")
            winu.destroy()
        elif userval == 0 and passval == 1:
            user = None
            passw = passe.get()
            main.update(site, user, passw)
            messagebox.showinfo("Success", "Password updated")
            winu.destroy()
        elif userval == 1 and passval == 1:
            user = str(usere.get())
            passw = str(usere.get())
            main.update(site, user, passw)
            messagebox.showinfo("Success", "Username and password updated")
            winu.destroy()
        elif userval == 0 and passval == 0:
            messagebox.showinfo("Error", "Please check a box")

    updatebtn = tkinter.Button(winu, text="Update", command=updatebt)
    updatebtn.grid(row=4, column=0)

    winu.mainloop()

The output is always 0,0 and the corresponding error box showing up. I have no such problems in a parallel code that I wrote:

import tkinter
from tkinter import Checkbutton
win = tkinter.Tk()
v1 = tkinter.IntVar()
v1.set(0)
v2 = tkinter.IntVar()
v2.set(0)
c1 = tkinter.Checkbutton(win, text="mf", variable=v1)
c1.grid(row=0,column=0)
c2 = tkinter.Checkbutton(win, text="yeet", variable=v2)
c2.grid(row=1,column=0)
def btfn():
    i1 = (v1.get())
    i2 = (v2.get())
    print(i1,i2)
bt = tkinter.Button(win, text="Display",command=btfn)
bt.grid(row=2,column=0)
win.mainloop()

Any help would be appreciated! (P.S. I put 'e' at the end of a variable name to denote Entry, 'l' for Label, etc)




Aucun commentaire:

Enregistrer un commentaire