for循环将东西插入到tkinter窗口中

时间:2011-04-11 15:30:59

标签: python user-interface for-loop tkinter

我有Tkinter程序,必须向窗口添加大量数据,所以我尝试编写for循环来处理它,但因为我必须使用字符串变量作为Tkinter的对象名称在对象上运行.insert()。我没有很好地解释这里的方法

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:]
  

这里是问题行,因为我的变量与已创建的条目对象的名称相匹配,但'name'实际上是一个字符串变量,所以它给出了错误“AttributeError:'str'对象没有属性'插入'“

                    name.insert(0,ordersDict[item])

这是整个班级。它创建了一个Tkinter窗口,并用一种​​装运屏幕填充它,所以所有条目都是需要一定数量的订单。我也很新,所以我知道我做了很多事情。

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)

1 个答案:

答案 0 :(得分:1)

看起来你有一个概念错误:在这个方法中,变量“name”在第一个列表的最后一行之前不存在。然后创建它,并指向一个普通的Python字符串 - 如果你在类的其他地方使用“name”变量,那么该方法中不存在变量。

要轻松修复现有代码,请尝试将变量称为“self.name”,而不仅仅是创建它的名称,并在此方法的最后一行使用: 而是self.name.insert(0,ordersDict[item])

self.前缀会将您的变量转换为实例变量,该变量在类的同一实例上的方法之间共享。

另外,在这个方法中,你甚至不需要连续三次循环的字典,只需在文本变量中插入从“line”中提取的相关值。