Home > OS >  How do I adjust this frame with scrollbar inside the black space?
How do I adjust this frame with scrollbar inside the black space?

Time:09-22

I want to fit this frame with scrollbar(refer the provided image) in the black space present in the provided image. How do I do that. The frame should totally cover the black space.

The code for program.The image

from tkinter import *
from tkinter import ttk
C = Tk()
C.maxsize(1200, 750)
C.geometry("1200x750")
C.title("Mainscreen")
style = ttk.Style()
style.theme_use('clam')

BG = PhotoImage(file="Mainscreen bg.png")
ML = PhotoImage(file="Music_label.png")

BG_label = Label(C, image=BG, border=0)
BG_label.place(x=0, y=0)

style.configure("Vertical.TScrollbar", gripcount=0,
                background="Cyan", darkcolor="gray6", lightcolor="LightGreen",
                troughcolor="Turquoise4", bordercolor="gray6", arrowcolor="gray6",arrowsize=15)


wrapper1= LabelFrame(C, width="1600", height="100", background="gray6",bd=0)
mycanvas = Canvas(wrapper1,background="gray6",borderwidth=0, highlightthickness=0, width=700, height=600)
mycanvas.pack(side=LEFT, expand="false", padx=0)


yscrollbar = ttk.Scrollbar(wrapper1, orient="vertical",command=mycanvas.yview)
yscrollbar.pack(side=RIGHT, fill="y")

mycanvas.configure(yscrollcommand=yscrollbar.set)

mycanvas.bind('<Configure>',lambda e: mycanvas.configure(scrollregion=mycanvas.bbox("all")))
myframe = Frame(mycanvas)
mycanvas.create_window((0,0), window=myframe, anchor="n")

wrapper1.pack(side=RIGHT,expand="false", padx=0, pady=200)

for i in range(50):
    Button(myframe, image=ML,bg="gray6",bd=0).pack()


mainloop()

EDIT: Music_Label Mainscreen bg

CodePudding user response:

So after trying to understand the underlying problem for a while I have reached the conclusion, that the problem is with the fact that the button are being drawn in the myframe and the myframe is outside the mycanvas which contains the scrollbar. So by changing the master widget for the button from myframe to mycanvas, the problem gets fixed now the scrollbar is adjacent to the buttons. BUT, Also now the button r shifted the side since while packing the wrapper1, you did side = RIGHT, so I would also suggest that you use place here instead of pack, since pack depends on the space available and is not reliable if you are using a Background for your GUI and want the buttons within a portion of it.

I have changed the following lines -:

Button(mycanvas, image=ML,bg="gray6",bd=0).pack() # Changed to mycanvas from myframe

and

wrapper1.place(x = {YOUR X, WHERE THE BOX STARTS}, y = {YOUR Y, WHERE THE BOX STARTS}) # Use place instead..

You can use pack also, and change the padx and pady arguments but it will be tricky to get it to work always as expected.

The fixed code is this -:

from tkinter import *
from tkinter import ttk
C = Tk()
C.maxsize(1200, 750)
C.geometry("1200x750")
C.title("Mainscreen")
style = ttk.Style()
style.theme_use('clam')

BG = PhotoImage(file="Mainscreen bg.png")
ML = PhotoImage(file="Music_label.png")

BG_label = Label(C, image=BG, border=0)
BG_label.place(x=0, y=0)

style.configure("Vertical.TScrollbar", gripcount=0,
                background="Cyan", darkcolor="gray6", lightcolor="LightGreen",
                troughcolor="Turquoise4", bordercolor="gray6", arrowcolor="gray6",arrowsize=15)


wrapper1= LabelFrame(C, width="1600", height="100", background="gray6",bd=0)
mycanvas = Canvas(wrapper1,background="gray6",borderwidth=0, highlightthickness=0, width=700, height=600)
mycanvas.pack(side=LEFT, expand=False, padx=0)


yscrollbar = ttk.Scrollbar(wrapper1, orient="vertical",command=mycanvas.yview)
yscrollbar.pack(side=RIGHT, fill="y", expand = False)

mycanvas.configure(yscrollcommand=yscrollbar.set)

mycanvas.bind('<Configure>',lambda e: mycanvas.configure(scrollregion=mycanvas.bbox("all")))
myframe = Frame(mycanvas)
mycanvas.create_window((0,0), window=myframe, anchor="n")

wrapper1.pack(expand=True, padx=0, pady=200) # Use place instead

for i in range(50):
    Button(mycanvas, image=ML,bg="gray6",bd=0).pack() # Change this to mycanvas from myframe


mainloop()
  • Related