2013-10-29 66 views
0

我會在這裏誠實地說,我玩的treeviews和ttk沒有真正瞭解它是如何工作的。不過,我收到了一些問題,經過谷歌搜索後,我無法找到解決問題的正確方法。我使用treeviews作爲列表框,因爲ttk沒有列表框元素。python ttk treeview問題

  • 1:問題1:不知何故,我總是得到一個額外的列,爲什麼?

    chat = ttk.Treeview(height="26", columns=("Nick","Mensaje","Hora"), selectmode="extended") 
    chat.heading('#0', text='Nick', anchor=W) 
    
    chat.heading('#1', text='Mensaje', anchor=W) 
    
    chat.heading('#2', text='Hora', anchor=W) 
    
    chat.column('#0', stretch=NO, minwidth=0, width=100) 
    
    chat.column('#1', stretch=NO, minwidth=0, width=510) 
    
    chat.column('#2', stretch=NO, minwidth=0, width=100) 
    
    chat.place(bordermode=OUTSIDE, x=5, y=45) 
    

但是,這增加了在最後一個額外的列,所以我不得不添加解決它:

chat.column('#3', stretch=NO, minwidth=0, width=0) 
  • 問題2:當我試圖插入項目treeview,我意識到我找不到一種方式來說明信息應該去哪裏。例如,我想要一個變量來填充column1,但另一個變量填充爲column2。至於我能去爲:

    chat.insert('', "end", '', text=message) 
    

但這隻會對column0添加的消息。我如何使它保存在column1而另一個變種保存在column0

  • 問題3:這真的是用ttk顯示列表框的最好方法嗎?

謝謝你的回答。

編輯:我是特林做這樣的事情:http://pdqi.com/w/Download/BLT/treeview1.gifhttp://zoomq.qiniudn.com/ZQScrapBook/ZqFLOSS/data/20100928164510/multicolumn_treeview_plastiktheme.png

回答

2

對於問題1:我建議你重寫代碼:

chat = ttk.Treeview(height="26", columns=("Mensaje", "Hora")) 

chat.heading('#0', text='Nick', anchor=W) 
chat.heading('Mensaje', text='Mensaje', anchor=W) 
chat.heading('Hora', text='Hora', anchor=W) 

chat.column('#0', stretch=NO, minwidth=0, width=100) 
chat.column('Mensaje', stretch=NO, minwidth=0, width=510) 
chat.column('Hora', stretch=NO, minwidth=0, width=100) 

對於問題2 :使用

chat.insert('', 'end', 'iid_1') 
chat.set('iid_1', 'Hora', 'your value') 

對於問題3:目前ttk中沒有列表框,但可以使用經典Tk小部件中的Listbox。

+0

哦......謝謝!那麼如果這是它的工作原理,可以在特定FontColour中設置列的信息而不影響整行? – Saelyth

+0

@Saelyth可能,沒有。 – user2036954