guyd
guyd

Reputation: 743

create `class` for tkinter `Entry`

I'm trying to create a class for generic Entry instances.

I use self.status list in order to change it value ( part of automatic update ).

But, something very odd happens and I can't find it origin:

A functions is created named status, in order to change each Entry value. When the name of that function is changed to any other- i get this error message :

Traceback (most recent call last):    ####updated for entire traceback ####
File "schedualer.py", line 271, in <module>
sched_entries.get_stat[r-1][c].set(sch_file[r][c])
TypeError: 'method' object is not subscriptable

Any ides why ?

Here's the class code.

class sched_entry (object):
  def __init__(self,master,r1,c1,offr,offc,bg1=["yellow"]):
    self.status=[]
    for r in range(r1):
        newrow=[]
        for c in range(c1):
            v=StringVar()
            z=Entry(master,relief=SUNKEN,width=cell_width,bg=bg1,textvariable=v,justify='center')
            z.grid(row=r+offr, column=c+offc)
            newrow.append(v)
        self.status.append(newrow)
  def status():           ###### when change to stat():   ---> I get the error
    return self.status

I get the error when it is called:

for r in range(1,tasks_total+1):   #update table with schdule.csv data
for c in range (len(sched_headers)-1):
    sched_entries.stat[r-1][c].set(sch_file[r][c]) ### error when method changed to "stat"

Upvotes: 0

Views: 268

Answers (2)

developer_hatch
developer_hatch

Reputation: 16224

Edit 2

Important: you forget to add the self parameter to the function defined in the class sched_entry.

The object in python that implement method __getitem__(), like dictionaries and lists, your problems comes when you say that:

self.status=[]

status is a list, so it has the __getitem__() but then you also say:

def status(self): 
    return self.status

and the problem occurs, you a method (in this case status), doesn't have the __getitem__(), so, you must rename one of those, you can do:

def get_status(self): #here goes the self parameter
    return self.status

Edit

You also, most important, after see the edit, forget to add the (), that is how python actually executes a method, if you don't add it, you just get the reference of the method, but not the execution and the result of it (in your case, the list status)

sched_entries.get_status()[r-1][c].set(sch_file[r][c])

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599628

You have two things called status: a method and an instance variable containing a list. One will override the other; you need different names.

Although I'm not sure why you want the method in the first place, since it just returns the variable. You should remove that method altogether.

Upvotes: 5

Related Questions