简体   繁体   中英

Why doesn't my scroll box appear at the top of the scroll bar?

I want my scrollbox to be at the top of the scrollbar. But it shows up in the middle of the scrollbar when I run the program as shown below:

https://i.stack.imgur.com/EfxMJ.png

I want my scrollbar to be at the top so that the first widget in the window is displayed. But for some reason I can't figure out, the scroll box appears in the middle. Here is my code. What must I do to fix this bug?

from tkinter import *

root = Tk()
root.geometry("640x480")

def scrollFunc(event):
mycanvas.config(scrollregion=mycanvas.bbox('all'))

mycanvas=Canvas(root)
mycanvas.pack(fill=BOTH, expand=True)

myframe = Frame(mycanvas)

mycanvas.create_window((0,0), window=myframe)

myscrollbar = Scrollbar(mycanvas, command=mycanvas.yview)
myscrollbar.pack(side=RIGHT, fill=Y)
mycanvas.config(yscrollcommand=myscrollbar.set)

for x in range(1, 101):
    Label(myframe, text="Label: "+str(x)).pack()


mycanvas.bind("<Configure>", scrollFunc)

root.mainloop()

You give a coordinate of 0,0 for the window, but you do not define an anchor point. The default anchor is "c" (or "center"). That means that the middle of your frame will be at 0,0.

The simplest solution is to set the anchor point to be "nw" (which stands for "northwest").

mycanvas.create_window(..., anchor="nw")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM