Brandon
Brandon

Reputation: 361

for loop to insert things into a tkinter window

I have Tkinter program that has to add a significant amount of data to the window so I tried to write a for loop to take care of it but since I have to use a string variable for the name of the object that Tkinter is running .insert() on the object. I didn't explain it very well here is the method

def fillWindow(self):
        global fileDirectory
        location = os.path.join(fileDirectory, family + '.txt')
        file = open(location, 'r')

        ordersDict = {}
        for line in file:
            (key, value) = line.split(':', 1)
            ordersDict[key] = value

        for key in ordersDict:
            ordersDict[key] = ordersDict[key][:-2]

        for item in ordersDict:
            if item[0] == '#':
                if item[1] == 'o':
                    name = 'ordered%s' %item[2:]

right here is the problem line because I have the variable that matches the name of the entry object already created but 'name' is actually a string variable so it gives me the error "AttributeError: 'str' object has no attribute 'insert'"

                    name.insert(0,ordersDict[item])

here is the entire class. It makes a Tkinter window and fills it with a sort of shipping screen so all the entries are for how many orders of a certain thing are needed. I'm also very new so I know that I do things the long way a lot.

class EditShippingWindow(Tkinter.Toplevel):

def __init__(self, student):

    Tkinter.Toplevel.__init__(self)
    self.title('Orders')

    family = student

    ## Window Filling

    ageGroupLabel = Tkinter.Label(self,text='Age Group')
    ageGroupLabel.grid(row=0,column=0)

    itemColumnLabel = Tkinter.Label(self,text='Item')
    itemColumnLabel.grid(row=0, column=1)

    costColumnLabel = Tkinter.Label(self,text='Cost')
    costColumnLabel.grid(row=0, column=2)

    orderedColumnLabel = Tkinter.Label(self,text='Ordered')
    orderedColumnLabel.grid(row=0, column=3)

    paidColumnLabel = Tkinter.Label(self,text='Paid')
    paidColumnLabel.grid(row=0, column=4)

    receivedColumnLabel = Tkinter.Label(self,text='Received')
    receivedColumnLabel.grid(row=0, column=5)



    #Item Filling

    column1list = ['T-Shirt (2T):$9.00', 'T-Shirt (3T):$9.00', 'T-Shirt (4T):$9.00',
                   'Praise Music CD:$10.00', ':', 'Vest L(Size 6):$10.00', 'Vest XL(Size 8):$10.00', 
                   'Hand Book (KJ/NIV):$8.75', 'Handbook Bag:$6.00', 'Memory CD (KJ/NIV):$10.00',
                   ':', 'Vest L(size 10):$10.00', 'Vest XL(Size 12):$10.00', 'Hand Glider (KJ/NIV/NKJ):$10.00',
                    'Wing Runner (KJ/NIV/NKJ):$10.00', 'Sky Stormer (KJ/NIV/NKJ):$10.00', 'Handbook Bag:$5.00',
                    'Memory CD (S/H/C):$10.00', 'Hand Glider Freq. Flyer:$8.00', 'Wing Runner Freq. Flyer:$8.00',
                    'Sky Stormer Handbook:$8.00' , ':', 'Uniform T-Shirt Size (10/12/14):$13.00',
                      'Uniform T-Shirt Size(10/12/14):$13.00', 'Uniform T-Shirt(Adult S / M / L / XL):$13.00',
                     '3rd & 4th Gr. Book 1 (KJ / NIV / NKJ):$8.75', '3rd & 4th Gr. Book 2 (KJ / NIV / NKJ):$8.75',
                     '4th & 5th Gr. Book 1 (KJ / NIV / NKJ):$8.75', '4th & 5th Gr. Book 2 (KJ / NIV / NKJ):$8.75',
                     'Memory CD 3rd & 4th Gr. Book (1/2):$10.00', 'Drawstring Backpack:$5.50']
    column1num = 1
    for item in column1list:
        num = str(column1num)

        (title, price) = item.split(':')

        objectName1 = 'column1row' + num
        objectName1 = Tkinter.Label(self,text=title)
        objectName1.grid(row=column1num, column=1)

        objectName2 = 'column1row' + num
        objectName2 = Tkinter.Label(self,text=price)
        objectName2.grid(row=column1num, column=2)
        column1num += 1

    #Ordered Paid Recieved Filler

    for i in range(32):
        if  i == 11 or i == 22 or i == 0 or i == 5:
            pass
        else:
            width = 10
            # First Column
            title1 = 'ordered' + str(i)
            self.title1 = Tkinter.Entry(self,width=width)
            self.title1.grid(row=i,column=3)
            #self.title1.insert(0, title1)

            #Second
            title2 = 'paid' + str(i)
            self.title2 = Tkinter.Entry(self,width=width)
            self.title2.grid(row=i,column=4)
            #self.title2.insert(0, title2)

            #Third
            title3 = 'received' + str(i)
            self.title3 = Tkinter.Entry(self,width=width)
            self.title3.grid(row=i,column=5)
            #self.title3.insert(0, title3)






    ## Methods

    def fillWindow(self):
        global fileDirectory
        location = os.path.join(fileDirectory, family + '.txt')
        file = open(location, 'r')

        ordersDict = {}
        for line in file:
            (key, value) = line.split(':', 1)
            ordersDict[key] = value

        for key in ordersDict:
            ordersDict[key] = ordersDict[key][:-2]

        for item in ordersDict:
            if item[0] == '#':
                if item[1] == 'o':
                    self.name = 'ordered%s' %item[2:]

                    self.name.insert(0,ordersDict[item])



    fillWindow(self)

Upvotes: 0

Views: 1232

Answers (1)

jsbueno
jsbueno

Reputation: 110311

It looks like you have a conceptual error there: inside this method, the variable "name" does not exist up to the last line on the first listing. Then it is created, and points to an ordinary Python string -- if you are using a "name" variable elsewhere on your class that variable does not exist inside this method.

For an easy fix of your existing code, try calling the variable as "self.name" instead of just name where it is created, and on your last line in this method use: self.name.insert(0,ordersDict[item]) instead.

The self. prefix will turn your variable into an instance variable, which is shared across methods on the same instance of the class.

On a side note, you don' t need even the dictionary much less three consecutive for loops on this method, just insert the relevant values you extract from "line" in your text variable.

Upvotes: 1

Related Questions