#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Listbox with Scrollbar


    Hi.
    New to Python so sorry if obvious question but after a day I can't work out the answer. The code below produces a scrollable listbox but I can't work out how to read the displayed value for use in an equation. I hope to have a few of these eventually.
    Here is my code:
    Code:
    from Tkinter import *
    master = Tk()
    
    scrollbarKG = Scrollbar(master)
    scrollbarKG.grid(row=0, column=1, sticky='NS')
    
    listboxKG = Listbox(master, selectmode=SINGLE, yscrollcommand=scrollbarKG.set, height=1, width='3')
    for i in xrange(50, 150):
        listboxKG.insert(END, i)
    listboxKG.grid(row=0, column=0)
    scrollbarKG.config(command=listboxKG.yview)
    mainloop()
    There will probably be a button to click once all data has been selected via the listboxes.
    Thanks in advance for any help.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Does this Scale widget reference help?

    http://effbot.org/tkinterbook/scale.htm
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0
    Does this Scale widget reference help?
    I'll take a look, this is all very new.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0
    Hum!

    Not really what I want. In this instance the idea was for the user to select their weight, anything between 50 and 150 Kgs. Their weight would then go into an equation.

    I was trying this as opposed to them entering their weight and then having to error check to ensure it was a integer etc.

    EDIT:
    A Spinbox may work but once again how do you get the value?

    Hope that gives more info.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    474
    Rep Power
    33
    Generally you bind ListboxSelect to a callback then use curselection()
    Code:
    from Tkinter import *
    
    def get_weight(event):
        index = listboxKG.curselection()  
        value = listboxKG.get(index)
        print index, value
    
    master = Tk()
    master.geometry("100x100")
    
    scrollbarKG = Scrollbar(master)
    scrollbarKG.grid(row=0, column=1, sticky='NS')
    
    listboxKG = Listbox(master, selectmode=SINGLE, yscrollcommand=scrollbarKG.set, height=5, width='3')
    for i in xrange(50, 150):
        listboxKG.insert(END, i)
    listboxKG.grid(row=0, column=0)
    listboxKG.bind('<<ListboxSelect>>', get_weight)
    
    scrollbarKG.config(command=listboxKG.yview)
    
    mainloop()
    If you install the PMW extension to Tkinter and use the ScrolledListBox you won't have to mess with adding scroll bars.
    Last edited by dwblas; December 21st, 2012 at 04:19 PM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0
    Like this:
    Code:
    from Tkinter import *
    import tkMessageBox
    
    master = Tk()
    def helloCallBack():
       tkMessageBox.showinfo( "Hello Python", var.get())
       
    var = StringVar()
    w = Spinbox(master, from_=50, to=150, textvariable=var, state="readonly")
    w.pack()
    
    e = Button(master, text='press', border='2', command = helloCallBack)
    e.pack()
    
    mainloop()
    A bit messy but proves a point I think so will try again tomorrow. If anyone has any ideas please chip in.

IMN logo majestic logo threadwatch logo seochat tools logo