Reputation: 247
I have a problem with a tkinter application that is being made using python 3. I tried to create a canvas widget, without using a frame container and I tried to add two scrollbar to my canvas, one vertical and another orizontal, here is the code:
class drawCavnas(object):
def __init__(self, master, width=500, height=500):
''' build the canvas object '''
# class attributes
self.master = master
self.cWidth = width
self.cHeight = height
# creating the canvas object
self.canvas = tk.Canvas(self.master, width=self.cWidth, height=self.cHeight, bg="green")
self.canvas.grid(row=0, column=1, sticky="nwes")
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
# creating the scrolling
self.scroll_x = tk.Scrollbar(self.master, orient="horizontal", command=self.canvas.xview)
self.scroll_x.grid(row=1, column=1, sticky="ew")
self.scroll_y = tk.Scrollbar(self.master, orient="vertical", command=self.canvas.yview)
self.scroll_y.grid(row=0, column=2, sticky="ns")
self.canvas.configure(yscrollcommand=self.scroll_y.set, xscrollcommand=self.scroll_x.set)
where the master is the window created using tk.Tk(). The problem here is that once I've ran the program and once the window is created the canvas toolbars' are disabled and I can't interact with them. I tried to resize the window but the canvas resize as well. So do you have a solution?
Upvotes: 2
Views: 997
Reputation: 386285
Why is the canvas scrollbar disabled in a tkinter window?
You must tell what part of the larger virtual canvas you want to be scrollable. You've set the scroll region to be bounded by the objects in the canvas (self.canvas.configure(scrollregion=self.canvas.bbox("all"))
). However, you've done that before you draw anything in the canvas so tkinter thinks there is nothing to scroll.
If you have a 500x500 canvas, but want to be able to draw on some larger area, you can hard-code the scrollregion.
For example, this will let you scroll around in a 2000x2000 area:
self.canvas.configure(scrollregion=(0,0,2000,2000)
Upvotes: 3