0

Hello and happy new year,

i'm trying to build a user interface and have a problem with the Tix.ScrolledListbox. (Python 2.6.5, Tix 8.4.3, Windows XP)

I wanted to use it to show items of varying number, depending on a previous choice made by the user.

It's a GIS thing: the user picks a layer from a ComboBox, presses a button and the Listbox shows all fieldnames retrieved from the attribute table. So for some layers there are 5, for others 30 fields. In principle it works.

But the scrollbar next to the listbox remains grey with no function.

In a small test snippet, where, after pressing a button, a random (0..100) number of items is shown in the listbox the scrollbar works.

I have no idea. Anybody had this before?

Edit: The following samplecode shows a not scrollable scrolledListbox when arcpy is imported

import Tix  
import random  
import arcpy       

class SampleApp(object):
  def __init__(self):
    self.window = Tix.Tk()
    #listbox
    self.lbx = Tix.ScrolledListBox(self.window, width = 30)
    self.lbx.listbox.insert(Tix.END, " ")
    self.lbx.listbox.bind("<<ListboxSelect>>", self.Choose)

    #button to generate new list
    self.btn = Tix.Button(self.window, text = "new list",
                          command = self.NewList)
    #label shows chosen list item
    self.lbl = Tix.Label(self.window, text = "   ", bg = "white")


    #pack
    self.btn.pack(pady = 10)
    self.lbx.pack(side="top", fill="both", expand=True, pady = 10)
    self.lbl.pack(pady = 10)

    self.window.mainloop()

  #function to generate new listbox items on button command
  def NewList(self):
    self.lbx.listbox.delete(0, Tix.END)
    r = random.randint(1, 30)
    for i in range(r):
        self.lbx.listbox.insert(Tix.END, i)

  #event to show selected item in label
  def Choose(self, event):
    widget = event.widget
    selection = widget.curselection()
    value = widget.get(selection[0])
    self.lbl.config(text = value)


 sa = SampleApp()
  • Welcome to SO. You should include some code for us to check where the problem is. Please reduce your code to the **minimal** script that reproduces your problem and post it. – joaquin Jan 02 '13 at 12:24
  • 1
    Thanks for your advice. I was not sure to post the code with my question, because I work with ArcGis (arcpy package and pyper) and I considered this somewhat special so maybe there are not many people who could work with it. Well, when I tried to prepare the code for a post I actually found the problem. When I drop the 'import arcpy' the scrollbar works, when I put it back in the scrollbar remains grey. – user1942658 Jan 07 '13 at 09:48
  • You might consider write your own answer if you solved your problem. By the way, building a minimal example will often make you find a mistake or solve your problem. – FabienAndre Jan 07 '13 at 15:08
  • 99% of the time trying to reduce your code to the minimal script that reproduces your problem leads to its solution – joaquin Jan 08 '13 at 20:09

1 Answers1

-1

Did you attach the scrollbar to the listbox?

from Tkinter import *

root = Tk()

scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)

listbox = Listbox(root)
listbox.pack()

for i in range(100):
    listbox.insert(END, i)

# attach listbox to scrollbar
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)

mainloop()

stolen from: http://effbot.org/zone/tkinter-scrollbar-patterns.htm

edit: Tix.ScrolledListBox works differently so don't mix it up with the above solution.

Gonzo
  • 2,023
  • 3
  • 21
  • 30
  • Thank you! I might have to try this. – user1942658 Jan 07 '13 at 10:15
  • I did try it. But there is still odd behaviour when I use this code with the import arcpy statement. Somehow the arcpy import messes up the listbox scrollbar connection. – user1942658 Jan 07 '13 at 11:58
  • I think this would only make the problem worse -- Tix.ScrolledListBox already has an attached scrollbar. – Bryan Oakley Jan 07 '13 at 12:00
  • No =) nothings worse. I only tried the suggested code above and added import arcpy. But maybe not everyone here has ArcGis and arcpy to try it too. – user1942658 Jan 07 '13 at 12:04