Aurorer D. Wysteria
Aurorer D. Wysteria

Reputation: 31

tkinter - Widgets are not showing with Scrollbar

I tried to add a scrollbar for my app using this video as an example: https://www.youtube.com/watch?v=0WafQCaok6g

When I tried it, with my widgets using place instead of pack, my widgets do not show up in the window. Anyone knows what is going on?

Below is the snippet from my code:

from tkinter import *
from tkinter import ttk

originY = 0

window = Tk()
window.title("Applications Management System")
window.geometry("100x100")

main_frame = Frame(window)
main_frame.pack(fill=BOTH, expand=1)

canvas = Canvas(main_frame)
canvas.pack(side=LEFT, fill=BOTH, expand=1)

scrollBar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=canvas.yview)
scrollBar.pack(side=RIGHT, fill=Y)

canvas.configure(yscrollcommand=scrollBar.set)
canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))

frame = Frame(canvas)

canvas.create_window((0, 0), window=frame, anchor="nw")

companyName = Label(frame, text="XYXYX Company", fg='purple').place(x=0, y=originY)
originY += 30

applicationFormTitle = Label(frame, text="JOB APPLICATION FORM", bg='purple', fg='white', width=90).place(x=0, y=originY)
originY += 30

# application details portion
posApplyingTitle = Label(frame, text="Position Applied").place(x=0, y=originY)
posApplyingTextField = Entry(frame).place(x=100, y=originY, width=200)

dateAppliedTitle = Label(frame, text="Date Applied").place(x=320, y=originY)
dateAppliedTextField = Entry(frame).place(x=420, y=originY, width=200)
originY += 30

...

outcomeTitle = Label(frame, text="Outcome").place(x=0, y=originY)
discardOption = Radiobutton(frame, text="Discard").place(x=100, y=originY)
keepForFutureOption = Radiobutton(frame, text="Keep for Future").place(x=250, y=originY)
originY += 30
insufficientDocOption = Radiobutton(frame, text="Insufficient Documents").place(x=100, y=originY)
callForInterviewOption = Radiobutton(frame, text="Call for Interview").place(x=250, y=originY)
offerWithoutInterviewOption = Radiobutton(frame, text="Offer without Interview").place(x=400, y=originY)
originY += 30

reasonTitle = Label(frame, text="Reason").place(x=0, y=originY)
reasonTextField = Entry(frame).place(x=100, y=originY, width=200)
originY += 30

dateOfReviewTitle = Label(frame, text="Date").place(x=0, y=originY)
dateOfReviewTextField = Entry(frame).place(x=100, y=originY, width=200)
originY += 30

window.mainloop()

When executed, it shows nothing in the window, like so: enter image description here

The scrollbar does not work too.

If anyone is able to help, it'll be greatly appreciated

Upvotes: 0

Views: 86

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386362

When you use pack or grid, the parent widget will grow or shrink to fit the children widgets. This does not happen when you use place. So, because you are using place, frame doesn't grow or shrink to fit the widgets that are inside of it. Because you didn't explicitly give frame a size, it defaults to a 1x1 pixel dot which makes it impossible to see.

If you choose to stick with using place, it is up to you to compute the appropriate size of frame. This is the main disadvantage to using place: it requires you to do a lot more work to get the widgets to look right.

The better option is to use pack and/or grid for widgets inside of frame. That will cause frame to fit itself to be exactly the right size. You can then bind to the <Configure> event of the frame to recompute the scrollregion of the canvas so that the scrollable region exactly fits the space required for frame and all of its children.

Upvotes: 2

Related Questions