0

I've am new to Tkinter and have written a program to open a file and parse binary messages.

I am struggling on how best to display the results. My parsing class will have 300+ entries and I want something similar to a table.

var1Label : var1Val

var2Label : var2Val

I have played around with these widgets but have not gotten anything that I can be proud of: Label, Text, Message and probably others.

So I'd like the Labels to be justify Right, and the Var's to be justify left or anything else that would that would be a good idea on how to make this an attractive display, like having all the ':' aligned. The size of the Var's will be between 0-15 characters.

I'm using python 2.7.2 on windows.

Here's the grid method I was trying with dummy variables

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)
ronalchn
  • 12,225
  • 10
  • 51
  • 61
chaps
  • 158
  • 1
  • 6
  • have you checked out the `.grid()` system? im assuming that you are using the `.pack()` method. – IT Ninja Sep 09 '12 at 03:32
  • Yes, I am using the .grid() system. I was looking for a widget that would allow me to set some properties on a column level. – chaps Sep 09 '12 at 23:38
  • I was having trouble responding so I edited my original post with a sample of how I was trying to use the .grid() system. That displays correctly but getting everything below to line up didn't seem to work for me and doing this 300+ times seemed cumbersome, although I would do it if I could figure out how to make everything align. – chaps Sep 09 '12 at 23:51
  • Don't 'do this 300+ times', just put it in a loop. You can also take a look at [this question](http://stackoverflow.com/questions/9348264/can-tkinter-created-an-application-with-tables) or [here](http://docs.python.org/dev/library/tkinter.ttk.html#treeview). – Junuxx Sep 10 '12 at 01:32
  • @Junuxx Thanks, I didn't know about the ttk extensions, I'll give the Treeview option a try. I didn't try looping over the .grid() method as I was having trouble getting the alignment right. Thanks. – chaps Sep 10 '12 at 02:21

1 Answers1

0

The ttk.Treeview widget lets you create a list of objects with multiple columns. It will probably be the easiest thing for you to use.

Since you specifically asked about a grid of labels, here is a quick and dirty example showing how to create 300 items in a scrollable grid:

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()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • This is pretty much exactly what I was looking for. I don't have enough points to give you the +1 but I can take this and expand it to multiple columns and pretty much run with it. – chaps Sep 27 '12 at 16:29