user3046180
user3046180

Reputation: 315

Python Tkinter: Attach scrollbar to listbox as opposed to window

Here is a screenshot of my window at present:

screenshot

My problem is that I simply cannot get the scrollbar to appear attached to the right side of the listbox instead of the right side of the main window. The code is here:

from Tkinter import *

def onselect(event):
    w = event.widget
    index = int(w.curselection()[0])
    value = w.get(index)
    info = find_info(value)
    listSelection.delete(0, END)
    listSelection.insert(END, "Node ID: " + info[0])
    listSelection.insert(END, "Owner/Description: " + info[1])
    listSelection.insert(END, "Last Latitude: " + info[2])
    listSelection.insert(END, "Last Longitude: " + info[3])



mapNodes = "http://ukhas.net/api/mapNodes"
nodeData = "http://ukhas.net/api/nodeData"
current_id = 0

window = Tk() # create window
window.configure(bg='lightgrey')
window.title("UKHASnet Node Manager")
window.geometry("680x400")

lbl1 = Label(window, text="Node List:", fg='black', font=("Helvetica", 16, "bold"))
lbl2 = Label(window, text="Node Information:", fg='black', font=("Helvetica", 16,"bold"))
lbl1.place(x=0, y=0)
lbl2.place(x=200, y=0)

scrollbar = Scrollbar(window, orient="vertical")
listNodes = Listbox(window, width=20, height=20, yscrollcommand=scrollbar.set, font=("Helvetica", 12))
scrollbar.config(command=listNodes.yview)
scrollbar.pack(side="right", fill="y")

listSelection = Listbox(window, width=50, height=4, font=("Helvetica", 12))

# pack objects onto window
listNodes.place(x=1, y=40)
listSelection.place(x=200, y=40)

Any help appreciated, and I'm running Ubuntu 14.04 and Python 2.7.5

Matt

Upvotes: 16

Views: 42960

Answers (2)

furas
furas

Reputation: 142651

You attached scrollbar to window

Scrollbar(window, orient="vertical")

Try to attache to listNodes

Scrollbar(listNodes, orient="vertical")

or create Frame with Listbox and attache scrollbar to that frame.


EDIT: example with Frame

from Tkinter import *

window = Tk()
window.geometry("680x500")

Label(window, text="Top label").pack()

frame = Frame(window)
frame.pack()

listNodes = Listbox(frame, width=20, height=20, font=("Helvetica", 12))
listNodes.pack(side="left", fill="y")

scrollbar = Scrollbar(frame, orient="vertical")
scrollbar.config(command=listNodes.yview)
scrollbar.pack(side="right", fill="y")

listNodes.config(yscrollcommand=scrollbar.set)

for x in range(100):
    listNodes.insert(END, str(x))

Label(window, text="Bottom label").pack()

window.mainloop()

enter image description here


EDIT: frame in your code - I use grid/pack because I prefered it.

I add some code so now lists resize when window resizes.

from Tkinter import *

def onselect(event):
    w = event.widget
    index = int(w.curselection()[0])
    value = w.get(index)
    info = find_info(value)
    listSelection.delete(0, END)
    listSelection.insert(END, "Node ID: " + info[0])
    listSelection.insert(END, "Owner/Description: " + info[1])
    listSelection.insert(END, "Last Latitude: " + info[2])
    listSelection.insert(END, "Last Longitude: " + info[3])



mapNodes = "http://ukhas.net/api/mapNodes"
nodeData = "http://ukhas.net/api/nodeData"
current_id = 0

window = Tk() # create window
window.configure(bg='lightgrey')
window.title("UKHASnet Node Manager")
window.geometry("680x400")

lbl1 = Label(window, text="Node List:", fg='black', font=("Helvetica", 16, "bold"))
lbl2 = Label(window, text="Node Information:", fg='black', font=("Helvetica", 16,"bold"))
lbl1.grid(row=0, column=0, sticky=W)
lbl2.grid(row=0, column=1, sticky=W)

frm = Frame(window)
frm.grid(row=1, column=0, sticky=N+S)
window.rowconfigure(1, weight=1)
window.columnconfigure(1, weight=1)

scrollbar = Scrollbar(frm, orient="vertical")
scrollbar.pack(side=RIGHT, fill=Y)

listNodes = Listbox(frm, width=20, yscrollcommand=scrollbar.set, font=("Helvetica", 12))
listNodes.pack(expand=True, fill=Y)

scrollbar.config(command=listNodes.yview)

listSelection = Listbox(window, height=4, font=("Helvetica", 12))
listSelection.grid(row=1, column=1, sticky=E+W+N)


for x in range(100):
    listNodes.insert(END, x)

for x in "ABCD":
listSelection.insert(END, x + ": ?")

enter image description here

Upvotes: 24

Bryan Oakley
Bryan Oakley

Reputation: 385970

Since you're using place (which I don't recommend), just do some math to calculate the position of the scrollbar.

The better choice in this specific case is to use grid, because you clearly want things organized in rows and columns. The header is row 0, and the listbox/scrollbar combination(s) are in row 1. The first header goes in columns 0 and 1, the listbox in column 0, and the scrollbar in column 1. The second header goes in column 2, and the other listbox goes in column 3.

Upvotes: 2

Related Questions