Python Tkinter: Attach scrollbar to listbox as opposed to window

后端 未结 2 1592
刺人心
刺人心 2020-12-30 09:23

Here is a screenshot of my window at present:

\"screenshot\"

My problem is that I simply cannot get the

相关标签:
2条回答
  • 2020-12-30 09:40

    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.

    0 讨论(0)
  • 2020-12-30 09:57

    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

    0 讨论(0)
提交回复
热议问题