Home > Net >  Why i can't put the 2 scrollbars inside the grey Listbox?
Why i can't put the 2 scrollbars inside the grey Listbox?

Time:05-03

Why i can't move the Frame with pady but with padx i can??

frame = Frame(window)
frame.pack(side=LEFT,pady=15)
lista_spese = Listbox(frame)
lista_spese.configure(font=("Ink Free",20), width=28, height=8, bg="#4a4a4a", fg="#dedede",relief="solid",borderwidth=4)
etichetta_lista_spese = Label(window,text="Lista delle spese",bg="#64d981",font=("Ink Free",19),relief="solid",borderwidth=1)
etichetta_lista_spese.place(relx=0.02, rely=0.27)
scrollbar = Scrollbar(frame,command=lista_spese.yview)
scrollbar.pack(side=RIGHT,fill=Y)
lista_spese.config(yscrollcommand=scrollbar.set)
scrollbar2 = Scrollbar(frame,command=lista_spese.xview,orient="horizontal")
scrollbar2.pack(side=BOTTOM,fill=X)
lista_spese.config(xscrollcommand=scrollbar2.set)
lista_spese.pack()

CodePudding user response:

Personally I think these types of problems are much easier to solve when you group all of the layout commands together for a given group of windows. So let's start by doing that:

lista_spese.pack(side=TOP)
scrollbar.pack(side=RIGHT,fill=Y)
scrollbar2.pack(side=BOTTOM,fill=X)

Now it becomes clear that you're first putting the listbox on the top, and then later adding the scrollbars. With pack, the order is important. The packer will allocate the entire side of a container to a widget. So, when you pack the listbox to the top, that allocates the entire top part of the empty frame for the listbox. By default, nothing else can go to the side or above, though that rule can be broken with additional arguments.

If you instead first pack the scrollbars, and then pack the listbox, everything works the way you expect.

scrollbar.pack(side=RIGHT,fill=Y)
scrollbar2.pack(side=BOTTOM,fill=X)
lista_spese.pack(side=TOP)

You can also keep the original order and use the before or after options to change the order in which the empty space is allocated:

lista_spese.pack(side=TOP)
scrollbar.pack(side=RIGHT,fill=Y, before=lista_spese)
scrollbar2.pack(side=BOTTOM,fill=X)

For a comprehensive example of how the packer works, with illustrations, see the following explanation on this site: https://stackoverflow.com/a/57396569/7432

CodePudding user response:

I rewrote some of this to make it easier for me to follow and troubleshoot. I think I solved your issue below. Let me know if this helps. The order in which I placed and then configured the scrollbar and listbox seems to fix the issue.

import tkinter as tk

window=tk.Tk()
window.config(bg="yellow")

window.geometry("1500x770")
window.state("zoomed")

frame = tk.Frame(window, bg="green")
frame.pack(side=tk.LEFT)

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

listbox = tk.Listbox(frame)
listbox.pack()

etichetta_lista_spese = tk.Label(window,text="Lista delle spese",bg="#64d981",font=("Ink Free",19),relief="solid",borderwidth=1)
etichetta_lista_spese.place(relx=0.02, rely=0.27)

listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)

scrollbar2 = tk.Scrollbar(frame, orient=tk.HORIZONTAL)
scrollbar2.pack(side=tk.BOTTOM, fill=tk.X)

listbox.config(xscrollcommand=scrollbar2.set)
scrollbar2.config(command=listbox.xview)

listbox.configure(font=("Ink Free",20), width=28, height=8, bg="#4a4a4a", fg="#dedede",relief="solid",borderwidth=4)
listbox.configure(font=("Ink Free",20), width=28, height=8, bg="#4a4a4a", fg="#dedede",relief="solid",borderwidth=4)

window.mainloop()
  • Related