2012-06-15 37 views
1

我有一個函數創建像這樣通過網格()項:(更換while循環使用功能,這應該只是告訴你它是什麼樣子以後)編輯入門

from Tkinter import * 

root = Tk() 

index=0 

while index < 10: 

    col0 = Text(root,width=20, height=1, bg='white') 
    col0.grid(row=index,column=0) 
    col0.insert('0.0',"name") 
    col1 = Text(root,width=20, height=1, bg='white') 
    col1.grid(row=index,column=1) 
    col1.insert('0.0',"attribute #1") 
    col2 = Text(root,width=20, height=1, bg='white') 
    col2.grid(row=index,column=2) 
    col2.insert('0.0',"attribute #2") 
    col3 = Text(root,width=20, height=1, bg='white') 
    col3.grid(row=index,column=3) 
    col3.insert('0.0',"counter") 
    index+=1 

root.mainloop() 

所以在特殊事件中調用函數並創建一個新條目(一個新行)。但是如果已經有這個事件的入口,我只想增加計數器。

事件名稱和他們的條目(索引)保存在字典中,所以我確實有行和列,但我現在怎麼能實際訪問col3?

我打算通過col3.get()獲取計數器,但它在每一行都被稱爲col3,所以我怎麼能指定它?

是否可以將col0,col1,col2等放入一種結構(如字典)中,所以通過col0 [name] .insert()...(name是唯一的)訪問它們?我試過,但那並沒有解決(這並不意味着它不可能,我希望,我只是相當新的python)

沒有人有任何建議或解決我的問題嗎?

+0

增加什麼反? – mgilson

+0

使用'作爲範圍(10)中的索引:'而不是索引<10:',而丟失索引= 0和索引+ = 1行,就像一個python指針一樣。 – Jdog

回答

1

您需要將引用保存到您所創建的所有文字部件:

colHeaders = ["name", "attribute #1", "attribute #2", "counter"] 
myTextWidgets = [] 
for index in range(10): 
    subList = [] 
    for i, header in enumerate(colHeaders): 
     text = Text(root,width=20, height=1, bg='white') 
     text.grid(row=index,column=i) 
     text.insert('0.0',header) 
     subList.append(text) 
    myTextWidgets.append(subList) 

這將允許您訪問每個插件通過它的行/列:

myTextWidgets[0][1].config(bg="purple") 

你可以很容易地添加這樣的結構的新行像這樣:

subList = [] 
for i, header in enumerate(colHeaders): 
    text = Text(root,width=20, height=1, bg='white') 
    text.grid(row=len(myTextWidgets),column=i) 
    text.insert('0.0', header) 
    subList.append(text) 
myTextWidgets.append(subList) 

有了這個結構,它不是名稱,它們是唯一的,但每個單元格的位置爲

+0

這個解決方案工作得很好,調整了一點,以適應我的程序,但列表中的列表是我的目標。非常感謝! – user1451340

2

你的字典主意很好。我在這裏提出了一個簡單的實現...(類是保持數據在函數調用之間持續存在的最佳方式)。

在此示例中,可以通過app.ObjGrid[(row,col)]訪問文本小部件,也可以通過它訪問如果您是通過方法進行操作的話,則可以使用self.ObjGrid[(row,col)]

import Tkinter as tk 

class TextGrid(tk.Frame): 
    def __init__(self,master): 
     tk.Frame.__init__(self,master) 
     self.ObjGrid={} 
     self.index=1 
     for i in xrange(10): 
      self.addRow() 

    def addRow(self,index=None): 
     """ 
     add a row of widgets at row=<index>. If index is omitted, add the next row 
     If index is on the grid already, prints "Increment counter?" since I don't 
     know what you mean by that 
     """ 

     if(index is None): 
      index=self.index+1 
      self.index+=1 
     if (index,0) in self.ObjGrid: 
      print "Increment counter?" #probably replace this code... 
      return 

     self.ObjGrid[(index,0)] = col0 = tk.Text(self,width=20, height=1, bg='white') 
     col0.grid(row=index,column=0) 
     col0.insert('0.0',"name") 
     self.ObjGrid[(index,1)] = col1 = tk.Text(self,width=20, height=1, bg='white') 
     col1.grid(row=index,column=1) 
     col1.insert('0.0',"attribute #1") 
     self.ObjGrid[(index,2)] = col2 = tk.Text(self,width=20, height=1, bg='white') 
     col2.grid(row=index,column=2) 
     col2.insert('0.0',"attribute #2") 
     self.ObjGrid[(index,3)] = col3 = tk.Text(self,width=20, height=1, bg='white') 
     col3.grid(row=index,column=3) 
     col3.insert('0.0',"counter") 

if __name__ == "__main__": 
    root=tk.Tk() 
    app=TextGrid(root) 
    app.grid(row=0,column=0) 
    app.addRow(3) #Shouldn't do anything 
    app.addRow() #Should add another row to the end. 
    print(type(app.ObjGrid[(2,3)])) #tkinter text widget. 
    root.mainloop() 

由於款式筆記...

from XXX import * 

通常是令人難以接受的。

import XXX as short_name_that_is_easy_to_remember 

通常是優選的。另外,在Tkinter做東西時,我發現從課堂開始最容易。構建一個小部件。將小部件更改爲應用程序是微不足道的,但將應用程序更改爲小部件幾乎是不可能的。構建小部件的最簡單方法是從Frame繼承,然後將其他小部件打包到該框架上,如上所示。

+0

我已經在我的代碼中完成了導入內容,對於類是一樣的;)上面的代碼只是爲了解釋我的問題,但仍然感謝您的回答。 – user1451340

1

不僅是可能的,我認爲這是最好的情況。這裏有一個簡單的方法:

attr_names = ['name', 'attribute #1', 'attribute #2', 'counter'] 
def make_text_field(row, col, attr_name): 
    text = Text(root, width=20, height=1, bg='white') 
    text.grid(row=row, column=col) 
    text.insert('0.0', attr_name) 

text_fields = {} 
for row, col in itertools.product(xrange(10), xrange(4)): 
    text_fields[row, col] = make_text_field(row, col, attr_names[col]) 

該商店中的每個字段text_fields獨立,具有(row, col)元組的關鍵。你也可以創建一個列表清單;既然你想訪問整個列,你可能更喜歡這一點。你可以預先分配的名單,如上使用product,但爲了簡單起見,這裏的另一種方法:

list_of_cols = list() 
for col in xrange(4): 
    col = list() 
    list_of_cols.append(col) 
    for row in xrange(10): 
     col.append(make_text_field(row, col, attr_names[col]))