How can I create buttons and labels in a canvas widget and make the whole set of widgets in the canvas scrollable?

0

Issue

How can I create buttons and labels in a canvas widget and make the whole set of widgets in the canvas scrollable?

I came across a _create() option in my IDE but I’m not sure how to use it. I tried entering Button for itemType but it didn’t work.
I tried create_window() but I’m not sure how to use it either.

My question is not how to use a loop and add the buttons, but how can I create widgets and place them in a canvas, but scrollable? Also is there a _create()? If yes, is there any way to create widgets using it?

Solution

How to create buttons and labels in a canvas widget and make the whole set of widgets in the canvas scrollable?

You have two choices:

  • use the canvas method create_window to add the buttons and labels to the canvas at specific coordinates
  • use the canvas method create_window to add a frame to the canvas, and then put your buttons and labels inside of the frame using pack or grid or place.

You then have to make sure you configure the scrollregion of the canvas to be large enough to include the widgets.

I came across a _create() option in my IDE but I’m not sure how to use it.

The following example shows how to use create_window to add a button to a canvas. The procedure is the same for any type of widget.

canvas = tk.Canvas(root)
canvas.pack(fill="both", expand=True)

the_button = tk.Button(canvas, text="Click me", ...)
canvas.create_window(10, 10, window=the_button, anchor="nw")
canvas.configure(scrollregion=canvas.bbox("all"))

Answered By – Bryan Oakley

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More