我无法使用按钮单击在画布上显示图像

发布于 2025-01-27 14:22:44 字数 865 浏览 0 评论 0原文

我无法将图像添加到画布中,我想要的是,当单击文件管理器打开的按钮时,我可以选择一个映像,然后单击“打开”以加载图像,

这是我的代码: -

canvas=Canvas(width=200, height=200)
browse=Button(text="Browse")


canvas.place(x=440, y=80)
browse.place(x=480, y=300)

global photos

def browseFiles():
    filename = filedialog.askopenfilename(initialdir = "/",
                                  title = "Select a File",
                                  filetypes = (("Text files",
                                                "*.jpg*"),
                                               ("all files",
                                                "*.*")))

   image = (Image.open(filename))
   resize_image = image.resize((200, 200))
   photos = ImageTk.PhotoImage(resize_image)
   canvas.create_image(100, 100, image=photos)


browse.config(command=browseFiles)

I am not able to add an image to a canvas, what I want is when clicked on the button the file manager opens and then I can select an image and click open to load the image

this is my code:-

canvas=Canvas(width=200, height=200)
browse=Button(text="Browse")


canvas.place(x=440, y=80)
browse.place(x=480, y=300)

global photos

def browseFiles():
    filename = filedialog.askopenfilename(initialdir = "/",
                                  title = "Select a File",
                                  filetypes = (("Text files",
                                                "*.jpg*"),
                                               ("all files",
                                                "*.*")))

   image = (Image.open(filename))
   resize_image = image.resize((200, 200))
   photos = ImageTk.PhotoImage(resize_image)
   canvas.create_image(100, 100, image=photos)


browse.config(command=browseFiles)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文