[Tkinter-discuss] Listbox size
Bryan Oakley
bryan.oakley at gmail.com
Tue Feb 13 18:19:44 EST 2018
If your actual goal is to scroll the listbox so that an item is centered,
you can first scroll the item as far as possible and then call the "see"
method. For example, if the result is in the first half of the items,
scroll all the way down (listbox.yview("end")). If it's in the second half,
scroll all the way up (listbox.yview(0)). Then call "see" and tkinter will
do its best to center it.
The documentation for the "see" method says this:
Adjust the view in the listbox so that the element given by *index* is
> visible. If the element is already visible then the command has no effect;
> if the element is near one edge of the window then the listbox scrolls to
> bring the element into view at the edge; otherwise the listbox scrolls to
> center the element.
Another trick would be to get the index of the item at the 0th pixel
location of the widget (eg: the_listbox.index("@0,0"), and another at the
pixel location for the height of the widget (ie: by using the result of the
winfo_height method of the widget). That will tell you how many visible
lines there are.
On Tue, Feb 13, 2018 at 4:36 PM, Bob van der Poel <bob at mellowood.ca> wrote:
> I know how to find the number of items. Just like you show.
>
> No, this is not the size of the box. I have maybe 2000 entries. I can set
> the current selection, etc with code like this:
>
> self.lb.select_clear(ACTIVE) # needed to un-hilite existing selection
> self.lb.activate(x) # make new item active
> self.lb.see(x-10) # force display from 10
> previous lines
> self.lb.select_set(x) # and select the new item
>
> If the listbox is 20 lines high, this works perfectly. But, if it is a
> different size, it doesn't center. In my case I don't know the size of box,
> so the "-10" is a not-to-intelligent guess.
>
> Thanks.
>
>
>
> On Tue, Feb 13, 2018 at 2:49 PM, Bhaskar Chaudhary <bha100710 at gmail.com>
> wrote:
>
>> Hi Bob
>>
>> Are you looking for the number of items in the listbox ?
>> If yes this should give you the number of items.
>>
>> print(len(listbox.get(0, END)))
>>
>> Given that each item occupies one line in the list box, i think it the
>> same as giving the height in lines.
>>
>> regards
>> Bhaskar
>>
>>
>> On 2/13/18, Bob van der Poel <bob at mellowood.ca> wrote:
>> > Is there a way to get the current height (hopefully in lines) of a
>> listbox?
>> > I'd like to center, vertically, the result of a search.
>> >
>> > --
>> >
>> > **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars ****
>> > Bob van der Poel ** Wynndel, British Columbia, CANADA **
>> > EMAIL: bob at mellowood.ca
>> > WWW: http://www.mellowood.ca
>> >
>>
>
>
>
> --
>
> **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars ****
> Bob van der Poel ** Wynndel, British Columbia, CANADA **
> EMAIL: bob at mellowood.ca
> WWW: http://www.mellowood.ca
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> https://mail.python.org/mailman/listinfo/tkinter-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20180213/4667a54b/attachment-0001.html>
More information about the Tkinter-discuss
mailing list