0

I'm trying to put an image inside the Canvas that has the same width as the Canvas but the height is according to the aspect ratio, and using a Scrollbar to scroll the same. Here's the relevant part of the code.

#Canvas and Scrollbar
img_canvas = Canvas(main_frame, height = height, width = width+15)
vsb = Scrollbar(img_canvas, orient = 'vertical')
vsb.pack(side = 'right', fill = 'y')
vsb.configure(command = img_canvas.yview)
canvas.config(yscrollcommand = vsb.set)
text.window_create("end", window = img_canvas)
text.insert("end", "\n")

#Insert Image into the Canvas
im = Image.open(str(opath)+"//Ques_"+str(temp)+".png")
w, h = im.size
im = im.resize((width, int((h/w)*width)), Image.ANTIALIAS)
img = ImageTk.PhotoImage(im)
ques = Label(img_canvas, image = img)
ques.image = img
ques.pack(side = 'left', expand = False)

The problem I am encountering is that the image expands completely in y, and hence can't be scrolled. I want to contain the part of the image that fits into the Canvas's dimensions and the rest can be scrolled.

astqx
  • 2,058
  • 1
  • 10
  • 21

1 Answers1

2

You have to use create_image to put an image on a canvas and have it be scrollable. You can't use pack or place or grid. You don't need to put the image in a label first, unless you specifically want it to be a label with a border. In that case, you need to use create_window to add the label to the canvas.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I just have one more doubt, since all of this is going into a `Text` widget, how am I supposed to put the `Scrollbar`? As far as I am aware `Frame` doesn't get inserted into `Text` and `Scrollbar` can't have `Canvas` as the parent. – astqx Aug 15 '20 at 21:30
  • 1
    @AST: a scrollbar can have a canvas as a parent. You don't want to do that if the scrollbar is scrolling the canvas. Your question is very unclear. You are putting an image in a label in a canvas in a text widget. Why not directly add the image to the text widget, or why are you adding a canvas to the text widget? What is your ultimate goal with all of these widgets? I'm afraid you're making things way more complicated than they need to be. – Bryan Oakley Aug 15 '20 at 23:16
  • @ Bryan Oakley, apologies for being unclear, and I really appreciate your help. Here is the link to an image , this has the layout that I desire. I am using a loop to insert the same layout (on the left) into the text widget one after the other, which can can ultimately be scrolled. – astqx Aug 16 '20 at 08:09
  • @ Bryan Oakley, after trying for a while, I succeeded by inserting a `Canvas` into the `Text`, followed by packing another `Text` widget on the left and a `Scrollbar` on the right, into the canvas. Then I inserted the image into the `Text`. Would love to know if you could suggest a more efficient way of doing the same. – astqx Aug 16 '20 at 09:45
  • @AST: I would love to provide a more efficient way of doing things, but it's not really clear what you're trying to do. I don't understand why you're using a text widget at all since you don't seem to be needing to input text. – Bryan Oakley Aug 16 '20 at 16:14
  • I really appreciate that, I found `Text` easier for my purpose of inserting widgets one after the other without doing much about their positioning, like I would have to do in a `Canvas`. But if you think canvas better, then I would have to work the entire part of the code again. – astqx Aug 26 '20 at 04:30
  • @AST: like I said, I don't quite understand what you're ultimately trying to do. The text widget is a great solution for scrolling a vertical stack of widgets. What I don't understand is why you think you need a text and a frame and a canvas. – Bryan Oakley Aug 26 '20 at 04:56
  • Alright, please correct me if I'm wrong, since I require a scrolling image to be inserted into the text, I have thought of inserting a canvas first (because I can't insert a frame), then onto the canvas, I have 2 choices, either put another canvas or a text having the image in it, also put the scrollbar for the same. I don't think the use of a frame is necessary. – astqx Aug 26 '20 at 05:53
  • 1
    @AST: you can insert any widget into a text widget: canvas, frame, etc. Plus, there is direct support for adding images without needing another widget to hold it. – Bryan Oakley Aug 26 '20 at 14:06
  • Thank you for the help, I'll consider making some edits and try to directly utilize the text widget. – astqx Aug 26 '20 at 15:06