Python Tkinter:将滚动条附加到列表框而不是窗口 [英] Python Tkinter: Attach scrollbar to listbox as opposed to window

查看:481
本文介绍了Python Tkinter:将滚动条附加到列表框而不是窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前窗口的屏幕截图:

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)

感谢任何帮助,我正在运行Ubuntu 14.04和Python 2.7.5

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

马特

推荐答案

您已将滚动条附加到window

Scrollbar(window, orient="vertical")

尝试附加到listNodes

Scrollbar(listNodes, orient="vertical")

或使用Listbox创建Frame并将滚动条附加到该框架.

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

编辑:带有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()

框-我使用grid/pack是因为我更喜欢它.

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

这篇关于Python Tkinter:将滚动条附加到列表框而不是窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆