【发布时间】:2020-08-15 19:44:55
【问题描述】:
我试图在Canvas 中放置一个与Canvas 具有相同宽度但高度取决于纵横比的图像,并使用Scrollbar 滚动相同的图像。这是代码的相关部分。
#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)
我遇到的问题是图像在y 中完全展开,因此无法滚动。
我想包含适合Canvas 尺寸的图像部分,其余部分可以滚动。
【问题讨论】:
-
canvas.create_window()就是你要找的东西 -
感谢您的快速回复!你能详细说明一下吗?我试过这个
img_canvas.create_window(0, 0, window = ques),但现在似乎什么都没有显示。 -
但是如果你使用标签只显示图像这也可能很有趣stackoverflow.com/a/43009579/13629335
-
@Atlas435:不一定。如果他们只想在画布上放置图像,您可以不使用
Label和create_window。
标签: python python-3.x tkinter tkinter-canvas