Hi, On Tue, 13 Feb 2018 17:11:47 -0700 Bob van der Poel <b...@mellowood.ca> wrote:
> Getting closer :) > > I asked the question wrongly. I need to determine the number of VISIBLE > lines in the listbox. Should be (as used in my previous example) visible_lines = lb.nearest(lb.winfo_height()) - lb.nearest(0) > > Using the .winfo_height() does return the height in pixels. Note: You do > have to make the widget visible before doing this, otherwise you just > get "1" returned. widget.update() works for this. I don't think the widget must actually be "visible"; it is only when the widget is initially created that the widget's dimensions are reported as 1*1 px. Calling update_idletasks() once after the widgets have been created should be sufficient to work around this. > > So, the next question is: what is the line size? That is definitely not needed for what you want to accomplish. > > Now, to make life much simpler :) I just played a bit and discovered > that if I make the listbox visible BEFORE the selection, it > automatically centers. So, my code now becomes: > > self.lb.update() > self.lb.select_clear(ACTIVE) # needed to un-hilite existing > selection self.lb.see(x) > self.lb.activate(x) > self.lb.select_set(x) > > Easy. And I don't need to worry about the size of the box! Here see() does not necessarily center the given index. It sometimes appears near the top or the bottom of the list. And that's just what the manpage promises: " 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. " > > Well, mostly. If the selection I want is already on the screen, then the > box doesn't scroll to center. I think I can live with that. Good, again that's exactly what the listbox's manpage promises :) If you want exactly predictable behavior, I think you will have to use yview() instead. Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. You humans have that emotional need to express gratitude. "You're welcome," I believe, is the correct response. -- Spock, "Bread and Circuses", stardate 4041.2 _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss