entries between labels dynamically

André Larsen Risnes risnes at alfanett.no
Fri Feb 15 16:42:07 EST 2002


Artur Skura <arturs at iidea.pl> wrote:
[...]
>
> Now can you suggest an easy, lazy way of doing this (with any
> toolkit) so things don't pile up and everything looks nice?
> 
Sure (with tkinter):


from Tkinter import *

text = "When is a %text% not like a %text%? When it's a %text%!"
root = Tk()

for part in text.split("%"):
    if part == "text":
        Text(root, width=8, height=1).pack(side=LEFT)
    else:
        Label(root, text=part).pack(side=LEFT)

root.mainloop()


This will align everything on one line, though. If you want to
"break lines" you'll have to construct sub-frames for each
line. I guess this would be easy if the text data is stored in
a line-oriented file, like:


for line in file.readlines():
    for part in text.split("%"):
        f = Frame(root)
        if part == "text"
            Text(f, width=8, height=1).pack(side=LEFT)
        else:
            Label(root, text=part).pack(side=LEFT)
        Label(f, ...)
        f.pack(side=TOP, anchor=W, fill=X, expand=YES)

(haven't tested this last part, though)

-- 
Regards, André Risnes




More information about the Python-list mailing list