2012-09-09 29 views
0

我已經是Tkinter的新手了,並且已經編寫了一個程序來打開文件並解析二進制消息。Tkinter顯示varLabel和價值乾淨

我正在努力如何最好地顯示結果。我的解析類將有300多個條目,我想要類似於表的東西。

var1Label : var1Val 

var2Label : var2Val 

我打了周圍的這些小部件,但沒有得到任何東西,我可以自豪:標籤,文本消息和可能其他人。

所以我希望標籤能夠證明是正確的,而Var的證明是有理由的,或者其他任何可能對如何讓這個吸引人的顯示器成爲好主意的東西,比如擁有所有的':'對齊。 Var的大小將在0-15個字符之間。

我在windows上使用python 2.7.2。

這裏的網格法我是用虛擬變量

self.lbVar1 = Label(self.pnDetails1, text="Var Desc:", justify=RIGHT, bd=1) 
self.lbVar1.grid(sticky=N+W) 
self.sVar1 = StringVar(value = self.binaryParseClass.Var1) 
self.Var1 = Label(self.pnDetails1, textvariable=self.sVar1) 
self.Var1.grid(row=0, column=1, sticky=N+E) 
+0

你檢查出'.grid()'系統?即時假設你正在使用'.pack()'方法。 –

+0

是的,我正在使用.grid()系統。我正在尋找一個能讓我在列級設置一些屬性的小部件。 – chaps

+0

我在迴應時遇到問題,所以我編輯了我的原始文章,並提供了我如何使用.grid()系統的示例。這顯示正確,但讓一切下面排隊似乎並沒有爲我工作,做這300多次似乎繁瑣,雖然我會這樣做,如果我能弄清楚如何使一切對齊。 – chaps

回答

0

ttk.Treeview小部件,您可以創建多列對象的列表嘗試。它可能是最容易使用的東西。

既然你特別問有關標籤的網格,這裏是展示如何在滾動的網格中創建300項的快速和骯髒的例子:

import Tkinter as tk 
class ExampleApp(tk.Tk): 
    def __init__(self): 
     tk.Tk.__init__(self) 

     # create a canvas to act as a scrollable container for 
     # the widgets 
     self.container = tk.Canvas(self) 
     self.vsb = tk.Scrollbar(self, orient="vertical", command=self.container.yview) 
     self.container.configure(yscrollcommand=self.vsb.set) 
     self.vsb.pack(side="right", fill="y") 
     self.container.pack(side="left", fill="both", expand=True) 

     # the frame will contain the grid of labels and values 
     self.frame = tk.Frame(self) 
     self.container.create_window(0,0, anchor="nw", window=self.frame) 

     self.vars = [] 
     for i in range(1,301): 
      self.vars.append(tk.StringVar(value="This is the value for item %s" % i)) 
      label = tk.Label(self.frame, text="Item %s:" % i, width=12, anchor="e") 
      value = tk.Label(self.frame, textvariable=self.vars[-1], anchor="w") 
      label.grid(row=i, column=0, sticky="e") 
      value.grid(row=i, column=1, sticky="ew") 

     # have the second column expand to take any extra width 
     self.frame.grid_columnconfigure(1, weight=1) 

     # Let the display draw itself, the configure the scroll region 
     # so that the scrollbars are the proper height 
     self.update_idletasks() 
     self.container.configure(scrollregion=self.container.bbox("all")) 

if __name__ == "__main__": 
    app = ExampleApp() 
    app.mainloop() 
+0

這幾乎是我正在尋找的東西。我沒有足夠的積分給你+1,但我可以把它擴展到多列,並且幾乎可以與它一起運行。 – chaps