Tkinter Display varLabel and value cleanly

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)

Answers


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()

Need Your Help

Unable to block contacts whose numbers do not start with“ +91” in call blocker android app

java android

This application works perfectly with the contacts whose numbers are saved with +91,bt fails for other numbers.How do I solve this problem? And how do I add the options for adding numbers manually ...

Autoscroll a UITableView

iphone objective-c uitableview

I have implemented a UITableView, which shows up fine as a subview in my iPhoneApp. Now this is supposed to be something similar to a tele prompter, so I would like to autoscroll this thing up, til...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.