0

I have made canvas in root (entire window) and want to make it canvas scroll horizontally. But I have a problem with my code that doesn't work. I am quite new to Python so be patient for my newbiee question.

I am also wondering how can i make this scrollbar wider. I have only found how to make it taller but not wider.

root = Tk()

canvas = Canvas(root, bg="blue", height=700, width=500)
canvas.grid(column=0, row=0)

scrollbar = Scrollbar(canvas, orient=HORIZONTAL, width=18)
scrollbar.config(command=canvas.xview)
canvas.config(xscrollcommand=scrollbar.set)
scrollbar.grid(column=0, row=9, pady=5, columnspan=4)

root.mainloop()
Michał
  • 61
  • 8

1 Answers1

1

The canvas needs to know what part of its larger virtual canvas should be accessible via scrolling. You do this by setting the scrollregion attribute. The scrollregion defines the coordinates of the upper-left and lower-right corners of the area to be scrollable.

For example, if you want the canvas to appear on the screen as a 500x700 area but want to scroll over to the right to see 2000x700, you should do this:

canvas.configure(scrollregion=(0,0,2000,700))

Note: it wasn't clear from your question how or if you were adding things to the canvas. The above example gives a fixed scrollregion. The more common way is to define the scrollregion after adding widgets by using bbox("all") to get the bounding box of all of the objects on the canvas, like so:

canvas.configure(scrollregion=canvas.bbox("all"))

I am also wondering how can i make this scrollbar wider. I have only found how to make it taller but not wider.

That is controlled by the use of pack or grid. Also, the scrollbar shouldn't be a child of the canvas, it needs to use the same parent as the canvas.

Here is your code with the necessary changes. I've also changed the import statement to be PEP8 compliant, and added some tick marks so that you can see the canvas as it scrolls.

import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root, bg="blue", height=700, width=500)
scrollbar = tk.Scrollbar(root, orient="horizontal", width=18, command=canvas.xview)
canvas.configure(scrollregion=(0,0,2000,700), xscrollcommand=scrollbar.set)

root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

canvas.grid(column=0, row=0, sticky="nsew")
scrollbar.grid(row=1, column=0, sticky="ew")

# this adds tick-marks every 100 pixels so that you can
# see the canvas scroll
for x in range(0, 2001, 100):
    anchor = "sw" if x < 100 else ("se" if x==2000 else "s")
    canvas.create_line(x, 700, x, 690, fill="red")
    canvas.create_text(x, 680, text=x, anchor=anchor)

root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Okay, I thought the code execute exactly what I wanted but there is probably something that I had missed. I tried this code and its display scrollbar exactly where I wanted, I can drag from the left to the right and it's looks nice but it's not scroll canvas. I still some widget that is in half cut off and can't get to this. I can see it on the bigger screen but I cannot access it via scrollbar. I have my widgets from column 0 to column 5 and in future I would like to add more. What can I add here to make it work? – Michał Aug 27 '20 at 08:36
  • I check it out on a new file and it is working fine but when I add inside canvas my widgets (5 colums of 5 listbox + 2 entry in each column) it stop working, why? – Michał Aug 27 '20 at 10:58
  • 1
    @Michał: I can't see your code so I have no idea why it doesn't work. Are you using `create_window` to add those widgets to the canvas? Also, if the widgets are cut off you are probably defining them outside of the scrollable region. The best way to define the `scrollregion` is after all of the widgets are created using `scrollregion=canvas.bbox("all")`. – Bryan Oakley Aug 27 '20 at 12:16
  • I edit main question and add my almost whole code (I skip parts that i think was not relevant and working fine). Thanks a lot for help – Michał Aug 27 '20 at 12:53
  • 1
    @Michał: you shouldn't have done that. You've drastically changed the question, making the current answers irrelevant. Plus, what you posted isn't a minimal example, and doesn't add anything new to the canvas. You can't scroll items added with `grid`. – Bryan Oakley Aug 27 '20 at 13:01
  • Okay, sorry, I am still quite new here and didn't know that. I will delete my edit and left it as it was before so anyone else with similar problem could use it. And lastly so what do you suggest me to make it work? Change grid to pack and that will make items scrollable? – Michał Aug 27 '20 at 13:15
  • 1
    @Michał: no, you can't use `grid` or `pack` or `place`. If you want to scroll widgets on a canvas, they have to be added with `create_window`. – Bryan Oakley Aug 27 '20 at 13:18