简体   繁体   中英

I can't resize the scrollbar in python tkinter canvas (it is too small, I set the width option, but it did not work)

I tried to add the scrollbars to my tkinter canvas

w=10000
h=10000
m=500
import tkinter
canvas=tkinter.Canvas(width=w,height=h,bg='white',scrollregion=[0,0,w,h])
canvas.pack()
from tkinter import*

this works:

vscrollbar = Scrollbar(canvas,orient="vertical",width=500)
vscrollbar.pack(side=RIGHT, fill=Y)
canvas.config(yscrollcommand=vscrollbar.set)
vscrollbar.config(command=canvas.yview)

but there is some problem, I think:

hscrollbar = Scrollbar(canvas,orient="horizontal",width=500) #does not work
hscrollbar.pack(side=BOTTOM, fill=X)
canvas.config(xscrollcommand=hscrollbar.set)
hscrollbar.config(command=canvas.xview)

You should not be putting the scrollbars inside the canvas. You need to put them outside the canvas. If you want them to appear inside, create a frame to hold the canvas and scrollbars, then turn off the border of the canvas. Since you have no other widgets, you can put the scrollbar in the root window.

There are other problems with this code. For one, you shouldn't use a wildcard import. You're already importing tkinter so you don't need to also do from tkinter import * . Remove the wildcard import, and make sure you prefix all tkinter commands and constants with "tkinter". Personally I find it a bit easier to do import tkinter as tk so the prefix is a bit easier to type (eg: tk.Canvas versus tkinter.Canvas ).

Also, you should also explicitly create a root window. There's also rarely a need to explicitly set the width of a scrollbar. And finally, when using the packer, the order in which you pack things matters. It's best to put all of your pack commands together so it's easier to visualize the layout.

Last but not least, you've given the canvas a width and height of 10,000. Unless you really do have a display that is 10000x10000 pixels you should give it something more reasonable. If your scrollregion is set to 10000x10000 and the actual canvas is 10000x10000 there's no need for scrollbars.

I've fixed all of those things but otherwise tried to leave your code intact:

w=10000
h=10000
m=500
import tkinter

root = tkinter.Tk()

canvas=tkinter.Canvas(root, width=400,height=400,bg='white',scrollregion=[0,0,w,h])

vscrollbar = tkinter.Scrollbar(root,orient="vertical")
canvas.config(yscrollcommand=vscrollbar.set)
vscrollbar.config(command=canvas.yview)

hscrollbar = tkinter.Scrollbar(root,orient="horizontal")
canvas.config(xscrollcommand=hscrollbar.set)
hscrollbar.config(command=canvas.xview)

hscrollbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
vscrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
canvas.pack(side=tkinter.LEFT, fill="both", expand=True)

root.mainloop()

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