I'm trying multiple labels, use Scrollbar. but, I made a mistake

Multi tool use


I'm trying multiple labels, use Scrollbar. but, I made a mistake
from tkinter import *
from tkinter import ttk
class TestScroll(object):
def __init__(self, master):
self.master = master
# list
self.list = Listbox(self.master, height=10)
self.list.grid(row=0, column=0, sticky=NSEW)
for x in range(50):
self.list.insert(END, str(x) + " row...")
# scroll
self.scroll = Scrollbar(self.master, command=self.list.yview)
self.scroll.grid(row=0, column=1, sticky=NSEW)
self.list.configure(yscrollcommand=self.scroll.set)
class TestScroll2(object):
def __init__(self, master):
self.master = master
# canvas
self.canvas = Canvas(self.master, height=200)
self.canvas.grid(row=0, column=0)
# frame
self.frame = Frame(self.canvas)
for x in range(50):
ttk.Label(self.frame, text='label' + str(x) + '...').grid(column=0, row=x)
self.canvas.create_window((0, 0), window=self.frame, anchor=NW)
# scroll
self.scroll = Scrollbar(self.master, command=self.canvas.yview)
self.scroll.grid(row=0, column=1, sticky=NSEW)
self.canvas.configure(yscrollcommand=self.scroll.set)
# button
Button(self.master, text="activate scroll", command=self.activate_scroll).grid(row=0, column=2)
print(self.canvas.bbox(ALL)) # why can't ?
def activate_scroll(self):
print(self.canvas.bbox(ALL)) # need write here ?
self.canvas.configure(scrollregion=self.canvas.bbox(ALL))
root = Tk()
TestScroll2(root)
root.mainloop()
Why couldn't the first print get the correct value?
self.master.update_idletasks()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
This would be a much clearer question if you mentioned what incorrect value you're talking about, and what you expected instead. I'll guess that you're getting a bounding box of 0 by 0 pixels - because that's the default size of the Frame you're adding to the Canvas, and it hasn't been given a chance to adjust to the size of its child widgets yet. A call to
self.master.update_idletasks()
will give it that chance.– jasonharper
17 mins ago