我不知道为什么我的滚动条实现不起作用。

huangapple go评论54阅读模式
英文:

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=&quot;left&quot;, fill=&quot;both&quot;, expand=True)

    scrollbar = tk.Scrollbar(root, orient=&quot;vertical&quot;, command=canvas.yview)
    scrollbar.pack(side=&quot;right&quot;, fill=&quot;y&quot;)

    canvas.configure(yscrollcommand=scrollbar.set)
    canvas.bind(&quot;&lt;Configure&gt;&quot;, lambda e: canvas.configure(scrollregion=canvas.bbox(&quot;all&quot;)))

    # display the image in the canvas
    canvas_image = canvas.create_image(0, 0, anchor=&quot;nw&quot;, image=image)
    canvas.config(scrollregion=canvas.bbox(&quot;all&quot;))

    # add a button to save the image
    save_button = tk.Button(canvas, text=&quot;Save&quot;, command=lambda: save_image(image))
    canvas.create_window(10, 10, anchor=&quot;nw&quot;, 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=&#39;p1.png&#39;)
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=&quot;left&quot;, fill=&quot;both&quot;, expand=True)

    scrollbar = tk.Scrollbar(canvas, orient=&quot;vertical&quot;, command=canvas.yview)
    scrollbar.pack(side=&quot;right&quot;, fill=&quot;y&quot;)

    canvas.configure(yscrollcommand=scrollbar.set)
    canvas.bind(&quot;&lt;Configure&gt;&quot;, lambda e: canvas.configure(scrollregion=canvas.bbox(&quot;all&quot;)))

    # display the image in the canvas
    canvas_image = canvas.create_image(0, 0, anchor=&quot;nw&quot;, image=image)
    canvas.config(scrollregion=canvas.bbox(&quot;all&quot;))

    # add a button to save the image
    save_button = tk.Button(canvas, text=&quot;Save&quot;).pack()
    canvas.create_window(10, 10, anchor=&quot;nw&quot;, window=save_button)


display_image(image)

root.mainloop()

Screenshot:

我不知道为什么我的滚动条实现不起作用。

huangapple
  • 本文由 发表于 2023年2月16日 11:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定