2011-04-11 49 views
0

我有Tkinter程序必須添加大量的數據到窗口,所以我試圖寫一個for循環來照顧它,但由於我必須使用字符串變量用於Tkinter在對象上運行.insert()的對象的名稱。我沒有解釋得很好這裏是方法for循環將東西插入tkinter窗口

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

這裏是整個類。它製作了一個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) 
+0

我會建議編寫一個運行並演示問題的小代碼示例。 – Fenikso 2011-04-11 16:03:42

+0

@Fenikso我添加了完整的類,它在 – Brandon 2011-04-12 15:09:14

回答

1

看起來你有一個概念錯誤:在這個方法中,變量「name」不存在直到第一個列表的最後一行。然後創建它,並指向一個普通的Python字符串 - 如果在類的其他地方使用「name」變量,則此方法中不存在該變量。

爲了您的現有代碼的一個簡單的辦法,嘗試調用該變量爲「self.name」,而不是僅僅將其命名爲創建地方,並在此方法中使用您的最後一行: self.name.insert(0,ordersDict[item])代替。

self.前綴會將您的變量變成一個實例變量,該實例變量在類的同一實例中的方法間共享。

在附註上,您甚至不需要在此方法上連續三次以上的字典,只需將您從「行」中提取的相關值插入文本變量即可。

+0

我將兩行改爲self.name ='ordered%s'%item [2:],然後self.name.insert(0,ordersDict [item]),仍然有同樣的錯誤。另外name只用在這個for循環中它假設要插入的條目是先前用不同的for循環和一個不同的字符串變量創建的 – Brandon 2011-04-11 16:36:30

+0

我沒有說你應該在這個清單上更改這兩個「名稱」變量。 Ypou ar enot顯示你所有的代碼,我想你有另一種方法,你創建一個「名稱」變量,指向一個Tkinter的字符串變量。這是應該被製作成實例屬性的那個。如果您更改'name ='命令%s'%item [2:]',以「self」作爲前綴,那麼您將以相同的方式覆蓋它(並獲得相同的錯誤) – jsbueno 2011-04-11 16:45:39

+0

更新了問題 – Brandon 2011-04-11 16:47:50