2013-04-05 68 views
0

我想創建一個輸入框網格,我可以編輯並保存到其他地方的文本文件,但每次運行我的代碼時,如果我將變量「 e「,我只能編輯最後製作的盒子。在Tkinter中創建一個輸入框網格

from Tkinter import * 

class Application(Frame): 

    def __init__(self, master): 
     Frame.__init__(self, master) 
     self.grid() 
     self.create_widgets() 

    def create_widgets(self): 
     self.TXTlist = open('txtlist.txt', 'r+') 
     self.row = self.TXTlist.readline() 
     self.row = self.row.rstrip('\n') 
     self.row = self.row.replace('characters = ', "") #should end up being "6" 
     self.columns = self.TXTlist.readline() 
     self.columns = self.columns.rstrip('\n') 
     self.columns = self.columns.replace('columns = ', "") #should end up being "9" 
     i = 0 
     x = 0 
     for i in range (int(self.row)): 
      for x in range (int(self.columns)): 
       sroot = str('row' + str(i) + 'column' + str(x)) 
       e = Entry(self, width=15) 
       e.grid(row = i, column = x, padx = 5, pady = 5, sticky = W) 
       e.delete(0, END) 
       e.insert(0, (sroot)) 
       x = x + 1 
      x = 0 
      i = i + 1 
root = Tk() 
root.title("Longevity") 
root.geometry("450x250") 
app = Application(root) 
root.mainloop() 

回答

0

我會將這些條目存儲在某種數據結構中,以便以後輕鬆訪問它們。列表的列表將很好地工作,爲此:

self.entries = [] 
    for i in range (int(self.row)): 
     self.entries.append([]) 
     for x in range (int(self.columns)): 
      ... 
      e = Entry(self, width=15) 
      self.entries[-1].append(e) 
      ... 

現在,你必須輸入框中的引用:

self.entries[row_idx][col_idx] 

你可以修改你想要的東西。

+0

感謝@mgilson,它的工作,現在我可以繼續遇到另一個錯誤! – Nivert9 2013-04-05 19:39:06

相關問題