Reputation:
When I create the scroll bar there seems to be no option to scroll even though the contents inside of it are bigger than the canvas. The canvas is inside a frame as I heard this is the way to do this properly. The relevant part of my code:
from tkinter import *
class UniversityProcessor:
def __init__(self):
self.root = Tk()
self.rootWidth, self.rootHeight = 1200, 1200
screenWidth, screenHeight = self.root.winfo_screenwidth(), self.root.winfo_screenheight()
xPosition, yPosition = (screenWidth/2) - (self.rootWidth/2), (screenHeight/2) - (self.rootHeight/2)
self.root.geometry("%dx%d+%d+%d"%(self.rootWidth, self.rootHeight, xPosition, yPosition))
self.root.title("University processor")
self.updateUniText()
self.root.mainloop()
def updateUniText(self):
self.textPixelTotal = 0
self.frame = Frame(self.root, bg = self.bg, width = self.rootWidth, height = self.rootHeight)
self.frame.pack()
self.canvas = Canvas(self.frame, bg = self.bg, width = self.rootWidth, height = self.rootHeight)
self.inner = Frame(self.canvas, bg = self.bg, width = self.rootWidth, height = self.rootHeight)
self.canvas.create_window((0, 0), window = self.inner, anchor = "nw")
for it in range(0, 50):
label = Label(self.inner, bg = self.bg, text = f"Title {it + 1}", font = ("Arial", 20, "bold"))
label.place(y = self.textPixelTotal, x = it)
self.canvas.update()
self.textPixelTotal += label.winfo_height()
self.inner.configure(height = self.textPixelTotal)
self.inner.place(x=0,y=0,anchor="nw")
self.scroll = Scrollbar(self.frame, orient = VERTICAL, command = self.canvas.yview)
self.scroll.pack(side = RIGHT, fill = Y)
self.canvas.configure(yscrollcommand = self.scroll.set)
self.canvas.bind("<Configure>", lambda e: self.canvas.configure(scrollregion = self.canvas.bbox("all")))
self.canvas.pack(side=LEFT, expand=True, fill=BOTH)
UniversityProcessor()
I have no idea if there's some kind of canvas property or scroll bar property I need to set but it fills the screen...
Scroll bar does appear, it's the correct size however no scrolling actually happens
Upvotes: 0
Views: 329
Reputation: 46669
You should not use place()
to put labels inside canvas. You need to create an internal frame inside the canvas and put all the labels inside that frame:
def updateUniText(self):
textPixelTotal = 0
self.frame = Frame(self.root, width=self.rootWidth, height=self.rootHeight)
self.frame.pack()
self.background = Canvas(self.frame, width=self.rootWidth, height=self.rootHeight)
# an internal frame inside canvas
self.internal = Frame(self.background)
self.background.create_window(0, 0, window=self.internal, anchor="nw")
# create labels inside the internal frame
for i in range(0,200):
label = Label(self.internal, bg=self.bg, text=f"Title {i+1}", font=("Arial", 20, "bold"))
label.pack(anchor="w")
self.background.update()
self.scroll = Scrollbar(self.frame, orient=VERTICAL)
self.scroll.pack(side=RIGHT, fill=Y)
self.scroll.config(command=self.background.yview)
self.background.config(width=self.rootWidth-20, height=self.rootHeight)
self.background.config(yscrollcommand=self.scroll.set, scrollregion=self.background.bbox("all"))
self.background.pack(side=LEFT, expand=True, fill=BOTH)
Upvotes: 1