Width of canvas and of frame is not working. Canvas with a scrollbar inside a frame is not working. The whole frame is not visible

Question:

I want a frame of width=320 on the left side of the window with a canvas in which I have to display data from the file.
There will be a number of widgets (data) on that canvas, so I want to also use a scrollbar in that.
When I used pack(), nothing is being displayed on the window- not even the frame with bg="sky blue" is visible. Please tell me what’s wrong. I’ve tried all combinations of fill=BOTH,expand=True, etc. etc. Still, I am unable to make what I want to.


root = Tk()
root.state('zoomed')   #zooms the screen to maxm whenever executed
root.geometry('550x650')
root.minsize(550,650)

#Frame inside root
ContactDetails_frame = Frame(root,width=320,bg='sky blue',bd=2, relief=RIDGE)
ContactDetails_frame.pack(side=LEFT,fill=BOTH)

#Canvas inside the frame
canvas = Canvas(ContactDetails_frame,width=320,bg='sky blue',scrollregion=(30,0,1000,1000))
canvas.pack(fill=BOTH,expand=True)

#Vertical scrollbar for canvas
scrollbar = Scrollbar(canvas,orient=VERTICAL)
scrollbar.pack(side=RIGHT,fill=Y,expand=True)

#Attach scrollbar to canvas
canvas.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=canvas.yview)

#Here are the widgets
FirstName = Label(canvas, text="First name: ", font=(12),bg='sky blue',fg='black')
canvas.create_window(10,175, window=FirstName)

MiddleName = Label(canvas, text="Middle name: ",font=(12),bg='sky blue',fg='black')
canvas.create_window(10,195, window=MiddleName)

LastName = Label(canvas, text="Last name: ",font=(12),bg='sky blue',fg='black')
canvas.create_window(10,215, window=LastName)

NickName = Label(canvas, text="Nick name: ",font=(12),bg='sky blue',fg='black')
canvas.create_window(10,235, window=NickName)

Note = Label(canvas, text="Note: ",font=(12),bg='sky blue',fg='black')
canvas.create_window(10,855, window=Note)


root.mainloop()
Asked By: Kartikeya

||

Answers:

Try giving height to the frame and canvas that might do you job
Note that the units of frame and canvas are different that means
a frame with width 320 is smaller than a canvas of width 320
due to different units . Similar , is the case with height

When you pack the scrollbar inside the canvas, the canvas will shrink to fit the scrollbar. There’s almost never a good reason to put the scrollbar inside the canvas as it will cover whatever objects are on the edge. Under most circumstances the canvas and the scrollbar need to have a common parent.

canvas = Canvas(ContactDetails_frame, ...)
scrollbar = Scrollbar(ContactDetails_frame, ...)

scrollbar.pack(side="right",fill=Y)   
canvas.pack(side="left", fill=BOTH,expand=True)
Answered By: Bryan Oakley