lundi 30 avril 2018

How can I retrieve the check box state in python?

I am still new to python, I am trying to create an inventory management program for my work. I am stuck in the part where the user has to return some items back to stock after it was sent but returned by the customer. Basically I generate a list with the order name and beside each label there is a check box. The user has to tick the box beside the item that was returned. The problem is I cannot seem to retrieve the values of each box.

This is my code:

 def send(var,d):
    x=[]
    for i in range(0,len(var)):
        x.append(var[i].get())
    print(x)

def create_list(d,p,s,q,w):

    import tkinter as tk
    master4 = tk.Tk()
    var={}
    var2=[]
    tk.Label(master4, text='Description').grid(row=0,column=0)
    tk.Label(master4, text='Part number').grid(row=0,column=1)
    tk.Label(master4, text='Serial number').grid(row=0,column=2)
    tk.Label(master4, text='Well').grid(row=0,column=3)
    for i in range(0,len(p)):
        tk.Label(master4, text=str(d[i])).grid(row=i+1,column=0)
        tk.Label(master4, text=str(p[i])).grid(row=i+1,column=1)
        tk.Label(master4, text=str(s[i])).grid(row=i+1,column=2)
        tk.Label(master4, text=w).grid(row=i+1,column=3)
        var[i]=tk.IntVar()
        tk.Checkbutton(master4, text="", variable=var[i]).grid(row=i+1,

                                                                   column=4)
    tk.Button(master4, text='Next',
              command=lambda:returns.send(var,0)).grid(row=i+2,column=2)

It is part of a class called returns. The result is always a list of zeroes no matter which boxes I tick. The curious thing is that I wrote this exact code in a separate stand alone module and it worked like a charm.

Here's the one that worked:

    def sends(v):
        x=[]
        for i in range(0,len(v)):
        x.append(v[i].get())
        print(x)

   import tkinter as tk
   master = tk.Tk()
   var={}
   tk.Label(master, text='Description').grid(row=0,column=0)
   tk.Label(master, text='Part number').grid(row=0,column=1)
   tk.Label(master, text='Serial number').grid(row=0,column=2)
   tk.Label(master, text='Well').grid(row=0,column=3)
   for i in range(0,3):
       tk.Label(master, text='Ali').grid(row=i+1,column=0)
       tk.Label(master, text='Ali').grid(row=i+1,column=1)
       tk.Label(master, text='ALI').grid(row=i+1,column=2)
       tk.Label(master, text='Well').grid(row=i+1,column=3)
       var[i]=tk.IntVar()
       tk.Checkbutton(master, text="", variable=var[i]).grid(row=i+1,
                                                            column=4)
       tk.Button(master, text='Next',
                           command=lambda:sends(var)).grid(row=i+2,column=2) 
       tk.Button(master, text='Cancel', 
                              command=master.destroy).grid(row=i+2,
                                                                column=1)
       master.mainloop()

Why is one working the the other not? Does it have to do with the fact that the first one is part of a class and not just a function? I would appreciate any help.




Aucun commentaire:

Enregistrer un commentaire