Question

I have a code, which creates an UI like this.
enter image description here

I want those page up/down buttons to be next to Page 1 label but I couldn't managed to do that. Only way I know with pack is the side option and it is not working well.

Second thing is, that scrollbar should be in that listbox. I know, I need to create a canvas then a frame in it. Then embed both listbox and scrollbar to them but I couldn't do that either.

This is my code.

class interface(tk.Frame):
    def __init__(self,den):
        self.pa_nu = 0  ##page number. Both used in labeling and result slicing
        self.lbl1 = tk.Label(den, text="keyword")
        self.lbl2 = tk.Label(den, text="Page %d" %(self.pa_nu+1))
        self.ent1 = tk.Entry(den, takefocus=True)
        self.btn1 = tk.Button(den, text="Search", command=self.button1)
        self.btn2 = tk.Button(den, text="Page Up", command=self.page_up)
        self.btn3 = tk.Button(den, text="Page Down", command=self.page_down)

        scrollbar = tk.Scrollbar(den)
        scrollbar.pack(side=RIGHT, fill=Y)
        self.lst1 = tk.Listbox(den, selectmode="SINGLE", width="40", yscrollcommand=scrollbar.set)
        self.lst1.bind("<Double-Button-1>", self.open_folder)
        scrollbar.config(command=self.lst1.yview)

        self.lbl1.pack(side="top")
        self.ent1.pack()
        self.btn1.pack(side="top")
        self.btn2.pack(side="right")
        self.btn3.pack(side="left")
        self.lbl2.pack(side="bottom",padx=65)
        self.lst1.pack(fill=BOTH)

    def button1(self):     
        pass #some stuff here

    def page_up(self):
        pass #some stuff here

    def page_down(self):
        pass #some stuff here

    def list_fill(self,i):
        pass #some stuff here

    def open_folder(self,event):
        pass #some stuff here
Was it helpful?

Solution

There are three geometry managers in tkinter: place (absolute position), pack (good for line of widgets, or simple layout) and grid (complex layout).

Grid is worth looking for the layout you are working on. If you keep going with pack, the usual way to achieve complex layout is to use intermediate frames. For instance, in the following picture, all widgets in frame1 are packed vertically, and horizontally in frame2.

enter image description here diagram with draw.io

Regarding the scrollbar, the usual way is again to use an intermediate frame (no need for a canvas). Here a snippet (copied from http://effbot.org/zone/tkinter-scrollbar-patterns.htm#listbox)

frame = Frame(root, bd=2, relief=SUNKEN)

scrollbar = Scrollbar(frame)
scrollbar.pack(side=RIGHT, fill=Y)

listbox = Listbox(frame, bd=0, yscrollcommand=scrollbar.set)
listbox.pack()

scrollbar.config(command=listbox.yview)

frame.pack() #or others...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top