Tkinter 的按钮图片属性问题

Button Picture Attribute issues with Tkinter

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

当我使用 Button image= 属性时,按钮不可点击并且有点灰化且没有图片。当我使用 Button text= 属性时,按钮是可点击的,文本会弹出,但 width 和 height 属性不会 运行。我在这里找不到类似的 issues/answers。

        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))加载照片时,出现回溯错误。我不太清楚为什么我的照片不在 scope/garbaged 或者为什么命令工作取决于照片保存。

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

谢谢你们,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])

我有点草率,但它有效。