【问题标题】:Button Picture Attribute issues with TkinterTkinter 的按钮图片属性问题
【发布时间】:2021-08-23 18:16:55
【问题描述】:

我遇到了一些在函数内部制作的迭代按钮的奇怪问题。我将按钮列表作为全局变量,照片保存在我的主要 python 文件夹中。

当我使用 Button image= 属性时,这些按钮无法点击,并且显示为灰色且没有图片。当我使用 Button text= 属性时,按钮是可点击的并且文本会弹出,但宽度和高度属性不会运行。我在这里找不到类似的问题/答案。

        from tkinter import *
        from tkinter import filedialog as fd
        from tkinter.messagebox import showinfo
        from PIL import Image, ImageTk

        def OnSomeClick():
        ---Some Code---
            if Orientation == 1:
                 for i in range(len(Ordered_xCoord)):
                 InitButtonPhoto = '{}.png'.format(i)
                 ButtonPhoto = PhotoImage(file=InitButtonPhoto)
                 ImageButtonList.append(Button(action_window, command=Command, bd=3, width=110,
                                          height=85, image=ButtonPhoto))
                 ImageButtonList[i].grid(row=Ordered_yCoord[i], column=Ordered_xCoord[i])
            #this top if statement is what runs with my test; the bottom is my original
            else:
                 for i in range(len(Ordered_xCoord)):
                 ImageButtonList.append(Button(action_window, 
                 image=PhotoImage(file='{}.png'.format(i)),
                                          command=Command, bd=3, width=85, height=110))
                 ImageButtonList[i].grid(row=Ordered_yCoord[i], column=Ordered_xCoord[i])

通过 PhotoImage(Image.open(file)) 加载照片时,出现回溯错误。我不太清楚为什么我的照片超出范围/垃圾,或者为什么命令的工作依赖于照片保存。

Traceback Error with Image.open:
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Users/.conda/envs/CoordinateMath/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/Users/PycharmProjects/CoordinateMath/TkinterWindow.py", line 163, in Submit
    ButtonPhoto = PhotoImage(file=InitButtonPhoto)
  File "/Users/.conda/envs/CoordinateMath/lib/python3.8/tkinter/__init__.py", line 4064, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/Users/.conda/envs/CoordinateMath/lib/python3.8/tkinter/__init__.py", line 4009, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "<PIL.PngImagePlugin.PngImageFile image mode=RGB size=990x765 at 0x11034C310>": no such file or directory

【问题讨论】:

  • 能否添加整个回溯错误?
  • “没有这样的文件或目录”似乎很不言自明。您提供了一个不存在的文件的路径。
  • @BryanOakley 即使使用直接路径和真实路径,它也会显示回溯错误,因为它不喜欢 PhotoImage(Image.open())
  • 这是一个不同的问题。您的问题是关于“没有此类文件”错误的回溯,而回溯的原因是您提供的文件名无效。另一个问题可能与此有关:stackoverflow.com/questions/16424091
  • 文件正在保存,所以我认为文件名是有效的。使用链接,您已发布。我已经尝试过了,但不清楚是否可以在具有可变数量按钮的 for 循环中执行此操作。

标签: python tkinter python-imaging-library


【解决方案1】:

谢谢你,Matiiss 和 BryanOakley!修复是最新评论的组合,并让我更改了图像列表的格式。

def onClick():
   global Image_List, Ordered_xCoord, Ordered_yCoord, ImagedButtonList
--some code---
   for i in range(len(Ordered_xCoord)):         
     y = myimage.save('{}.png'.format(i))
     z = ImageTk.PhotoImage(Image.open('{}.png'.format(i)))
     Image_List.append(z)
---more code----
   if PaperOrientation == 1:
        for i in range(len(Ordered_xCoord)):
            InitButtonPhoto = Image_List[i]
            ButtonPhoto = InitButtonPhoto
            ImageButtonList.append(Button(action_window, command=Command, bd=3, width=110,
                              height=85, image=ButtonPhoto))
            ImageButtonList[i].grid(row=Ordered_yCoord[i], column=Ordered_xCoord[i])
    else:
        for i in range(len(Ordered_xCoord)):
            InitButtonPhoto = Image_List[i]
            ButtonPhoto = InitButtonPhoto
            ImageButtonList.append(Button(action_window, command=Command, bd=3, width=85,
                              height=110, image=ButtonPhoto))
            ImageButtonList[i].grid(row=Ordered_yCoord[i], column=Ordered_xCoord[i])

我有点草率,但它有效。

【讨论】:

    猜你喜欢
    • 2013-03-28
    • 2018-08-28
    • 2017-01-10
    • 2021-05-29
    • 2020-11-18
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多