473,479 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to align the text of a Listbox to the right

Greetings gentlemen and ladies,
I have a question: in Tkinter, how to align a Listbox entry (i.e. a
line of text) to the right?
Google did not show up the answer to my request.
Thanks very much.

L

Apr 27 '06 #1
3 12043
On 27 Apr 2006 02:35:50 -0700, Leonardo da Vinci
<le***********************@gmail.com> wrote:
Greetings gentlemen and ladies,
I have a question: in Tkinter, how to align a Listbox entry (i.e. a
line of text) to the right?


In a real Listbox, the answer is simple: you can't.

What are you trying to do? Maybe there is another widget more suited to
your needs.
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Apr 27 '06 #2
I have to use a Listbox that shows a list of entries. Every entry is a
char string quite long in size and I cannot set "width" to a large
value due to limitations of screen resolution. The rightmost part is
more important, so I thought that I could show only the end of the
string by aligning the field to the right.

Apr 27 '06 #3
On 27 Apr 2006 07:00:36 -0700, Leonardo da Vinci
<le***********************@gmail.com> wrote:
I have to use a Listbox that shows a list of entries. Every entry is a
char string quite long in size and I cannot set "width" to a large
value due to limitations of screen resolution. The rightmost part is
more important, so I thought that I could show only the end of the
string by aligning the field to the right.


As I see it, there are two solutions to your problems:
- Display your strings differently. It may seem stupid, but there are
sometimes situations where it is the best solution. Think about Apple's
way of displaying full file path names in menus: they put the file base
name first, then the full directory after a separator. If you have a way
to do that, your problem is solved.
- Don't use a Listbox to display your list, but another widget. A simple
Text with no wrap option may be a solution; setting a tag with the option
justify=LEFT on your whole text would do the trick. But if your lines of
text should be selectable, it may not be the right solution. In this case,
another widget you can use is a Canvas. It's a little trickier but can be
done. Here is an example:

--------------------------------------
from Tkinter import *

root = Tk()

cnv = Canvas(root, width=150, height=250)
cnv.grid(row=0, column=0, sticky='nswe')
hscroll = Scrollbar(root, orient=HORIZONTAL, command=cnv.xview)
hscroll.grid(row=1, column=0, sticky='we')
vscroll = Scrollbar(root, orient=VERTICAL, command=cnv.yview)
vscroll.grid(row=0, column=1, sticky='ns')
cnv.configure(xscrollcommand=hscroll.set, yscrollcommand=vscroll.set)

xMax = 0
y = 0
for i in range(20):
lbl = Label(cnv, text='Very, very long item number %s' % i)
cnv.create_window(150, y, window=lbl, anchor='ne')
y += lbl.winfo_reqheight()
xMax = max(xMax, lbl.winfo_reqwidth())

cnv.configure(scrollregion=(150 - xMax, 0, 150, y))

root.mainloop()
--------------------------------------

Putting bindings on the Label widgets created in the loop can be used to
simulate the behaviour of a Listbox.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Apr 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5085
by: Stan Brown | last post by:
http://www.acad.sunytccc.edu/instruct/sbrown/stat04/qz05_.htm A table comes near the beginning of the page. I have the <td> of row 1 column 1 styled in line with border-top:none and...
8
19985
by: André Hänsel | last post by:
Hi! When I put a table into a table and try to align it right, it works in IE but not in Firefox. Simplest code to reproduce: <html> <body> <table style="width: 100%"> <tr>
1
10866
by: Astra | last post by:
Hi All This has been bugging me for months and I just can't my finger on why it won't work in Safari/FireFox (IE lets me off cos it's nice liek that). In essence, the offending code is as...
2
18875
by: Tom McL. | last post by:
I would like to align text in a ListBox to achieve columns. In the past I could determine the text width in Twips and pad in required number of blank spaces. But now I'm using VB2005 and it...
3
349
by: Sori Schwimmer | last post by:
For a listbox, I would give a width and go with string formatting. In your case, I guess that what I'll do is to limit the width to something acceptable, and show only the tail of the line. ...
0
1399
by: Sori Schwimmer | last post by:
Eric Brunel is right in that a Listbox won't provide any builtin help for this kind of things. Suppose you can afford a width of 10 chars. Suppose your lines are stored in the variable Lines - a...
3
4395
by: acunnon | last post by:
I am trying to put together an login page my problem is getting the three items aligned to the middle verticaly without specifing a height to anything on the page. CSS html{ height:100%;...
13
3265
by: Stevo | last post by:
I've found that for IE6+, if you add the property text-align:center to a DIV, then *anything* inside it gets centered. That can be a table, an object/embed, another DIV, an image, or some text. ...
3
13261
by: happyse27 | last post by:
Hi All, I wanted to align the text box for user registration but the code just wont budge... Kindly advise what is wrong? Cheers... Andrew <HTML>
0
7027
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7019
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6719
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
166
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.