英文:
I do not know why my implementation of this scroll bar will not work
问题
我正在制作一个图像深炸器,需要一个滚动条,因为如果图像大于500x500,那么下载按钮将不会显示,因为图像占据整个窗口。
我尝试了以下的实现,但它没有起作用:
用Tkinter显示给定图像的函数
def display_image(image):
# 将图像转换为Tkinter格式
image = ImageTk.PhotoImage(image)
# 创建带有滚动条的画布
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack(side="left", fill="both", expand=True)
scrollbar = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
scrollbar.pack(side="right", fill="y")
canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
# 在画布中显示图像
canvas_image = canvas.create_image(0, 0, anchor="nw", image=image)
canvas.config(scrollregion=canvas.bbox("all"))
# 添加一个保存图像的按钮
save_button = tk.Button(canvas, text="保存", command=lambda: save_image(image))
canvas.create_window(10, 10, anchor="nw", window=save_button)
注意:请确保你的代码中的中文引号已更正为英文引号,以确保代码正常运行。
英文:
I am making an image deepfryer and need a scrollbar because if the image is bigger than 500x500, then the download button will not display as the image takes up the entire window.
I tried the following implementation and it did not work
function to display the given image using Tkinter
def display_image(image):
# convert the image to Tkinter format
image = ImageTk.PhotoImage(image)
# create a canvas with a scrollbar
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack(side="left", fill="both", expand=True)
scrollbar = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
scrollbar.pack(side="right", fill="y")
canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
# display the image in the canvas
canvas_image = canvas.create_image(0, 0, anchor="nw", image=image)
canvas.config(scrollregion=canvas.bbox("all"))
# add a button to save the image
save_button = tk.Button(canvas, text="Save", command=lambda: save_image(image))
canvas.create_window(10, 10, anchor="nw", window=save_button)
答案1
得分: -1
您的滚动条需要将Frame
作为canvas
,而不是root
。
这是您在我的示例中看到的解决方法。滚动条已激活,您可以看到。
代码段:
import tkinter as tk
root = tk.Tk()
image = tk.PhotoImage(file='p1.png')
def display_image(image):
# 将图像转换为Tkinter格式
# 创建带有滚动条的画布
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack(side="left", fill="both", expand=True)
scrollbar = tk.Scrollbar(canvas, orient="vertical", command=canvas.yview)
scrollbar.pack(side="right", fill="y")
canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
# 在画布中显示图像
canvas_image = canvas.create_image(0, 0, anchor="nw", image=image)
canvas.config(scrollregion=canvas.bbox("all"))
# 添加一个保存图像的按钮
save_button = tk.Button(canvas, text="保存").pack()
canvas.create_window(10, 10, anchor="nw", window=save_button)
display_image(image)
root.mainloop()
屏幕截图:
英文:
Your scrollbars
need to have the Frame
as a canvas
, not the root
.
This is what you see my example as I so workaround. The scrollbar
is activated That you can see.
Snippet:
import tkinter as tk
root=tk.Tk()
image = tk.PhotoImage(file='p1.png')
def display_image(image):
# convert the image to Tkinter format
# create a canvas with a scrollbar
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack(side="left", fill="both", expand=True)
scrollbar = tk.Scrollbar(canvas, orient="vertical", command=canvas.yview)
scrollbar.pack(side="right", fill="y")
canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
# display the image in the canvas
canvas_image = canvas.create_image(0, 0, anchor="nw", image=image)
canvas.config(scrollregion=canvas.bbox("all"))
# add a button to save the image
save_button = tk.Button(canvas, text="Save").pack()
canvas.create_window(10, 10, anchor="nw", window=save_button)
display_image(image)
root.mainloop()
Screenshot:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论