2

I want to show some items in listbox. Some are very short and some are very large(10-15 characters). Like below one,

1. facebook  gowtham95****@gmail.com

2. gmail  someone@gmail.com

3. stackoverflow   stackusername

I want to show them neatly like...

1. facebook    gowtham95****@gmail.com

2. gmail       someone@gmail.com

3. stack..     stackusername

Is there anyway to fix the column width in listbox in Tkinter?

and onemore thing, I want to label it above as id, domain and username. I don't want to use labels. I want to mention it in listbox itself, is there anyway?

gwthm.in
  • 638
  • 9
  • 24
  • Maybe try [TkinterTreeCtrl](http://tkintertreectrl.sourceforge.net/). See screenshots: [TkTreeCtrl](http://tktreectrl.sourceforge.net/) – furas Jun 08 '14 at 00:25
  • I just tried your suggestion. Thank you for that. But, in windows I'm unable to do that using tcltreectrl library. Imported successfully. But, while creating listbox, it's saying tcl error: can't find package tcltreectrl any suggestions ??? – gwthm.in Jun 08 '14 at 01:02
  • listboxes don't have columns, so no, there is no way to fix the width of a column in a listbox. How are you trying to create the "columns"? Are you actually using the treeview widget? (http://www.tkdocs.com/tutorial/tree.html) – Bryan Oakley Jun 08 '14 at 01:08
  • No, I'm using spaces. I import data from sqlite3 database file. "for row in cursor: row[0] + ' ' * 10 + row[1] – gwthm.in Jun 08 '14 at 01:11

1 Answers1

1

You could use string formating - but it requires some monospaced font, and you have to find the longest string in column(s).

import Tkinter as tk

master = tk.Tk()
master.geometry('600x100')

lb = tk.Listbox(master, font='monospace') # some monospaced font
lb.pack(fill=tk.BOTH, expand=1)

#---

data = [
    ('1', 'facebook', 'gowtham95****@gmail.com'),
    ('2', 'gmail', 'someone@gmail.com'),
    ('3', 'stackoverflow', 'stackusername')
]

longest_1 = max( len(x[1]) for x in data )
longest_2 = max( len(x[2]) for x in data )

for x in data:
    line = '%s | %*s | %*s |' % (x[0], -longest_1, x[1], -longest_2, x[2])    
    lb.insert(tk.END, line)

#---

tk.mainloop()

enter image description here

furas
  • 134,197
  • 12
  • 106
  • 148
  • Need to know, one thing. If in case, if a entry length is more, like "somethingtobedisplayeed" , I want it to be displayed as "somethin..". Is it possible with the above one? – gwthm.in Jun 08 '14 at 21:07
  • You have to change "somethingtobedisplayeed" to "somethin.." on your own before you insert it into Listbox. http://stackoverflow.com/questions/2872512/python-truncate-a-long-string – furas Jun 08 '14 at 21:53
  • how to use treectrl or named something? I'm getting error after installing. – gwthm.in Jun 08 '14 at 22:42
  • I didn't use it. Did you install [TkTreeCtrl](http://tktreectrl.sourceforge.net/) ? Tkinter is wrapper on Tcl/Tk so you need to install TkTreeCtrl (for Tcl) and TkinterTreeCtrl (for Python) to use it. – furas Jun 08 '14 at 22:52
  • Yeah, I installed it and imported successfully too. But when I used it in my program I got the error. – gwthm.in Jun 08 '14 at 23:04
  • I found only this http://stackoverflow.com/questions/23984676/tkinter-cant-find-package-treectrl - see in comments – furas Jun 08 '14 at 23:09
  • I read it. One more question, under windows my menu background or foreground colour is not changing. For the same program, when I run in on linux, it changed and working according to my program. Is there anything I need additionally in windows ? – gwthm.in Jun 08 '14 at 23:12
  • I don't know - I don't use Windows. Maybe ask [Bryan Oakley](http://stackoverflow.com/users/7432/bryan-oakley) - he is Master of Tkinter :) – furas Jun 08 '14 at 23:23
  • I use linux. I'm just making a windows executable. Thanks for helping me. – gwthm.in Jun 08 '14 at 23:30