python listbox滚动条_Python Tkinter:将滚动条附加到列表框而不是窗口

在Python Tkinter中,创建一个带有滚动条的列表框时,通常需要将滚动条与列表框关联,而不是与主窗口关联。问题在于滚动条出现在窗口的右侧,而非列表框的右侧。通过将滚动条与Listbox对象直接关联或创建一个包含Listbox和滚动条的Frame,可以解决这个问题。示例代码展示了如何正确地实现这一功能,并允许列表框随着窗口大小的改变而自动调整大小。
摘要由CSDN通过智能技术生成

Here is a screenshot of my window at present:

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

解决方案

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()

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 + ": ?")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值