【问题标题】:Don't see images on my buttons if I create them with a loop如果我使用循环创建按钮,则看不到按钮上的图像
【发布时间】:2016-07-14 10:14:41
【问题描述】:

我正在尝试在 Python 3.3.0 中编写一个程序来使用 Tkinter 进行训练,但是当我尝试将图像放在循环创建的按钮上时,我得到了一些不起作用的按钮(我可以'不要点击它们,它们没有图像),最后一个正在工作并且上面有图像。代码如下:

elenco = [immagine1, immagine2, immagine3, immagine 4]
class secondWindow:

    def __init__(self):

        self.secondWindow = Tk()
        self.secondWindow.geometry ('500x650+400+30')

class mainWindow:

    def __init__(self):

        self.mainWindow = Tk()
        self.mainWindow.geometry ('1100x650+100+10')
        self.mainWindow.title('MainWindow')

    def Buttons(self, stringa):

        i = 0
        for _ in elenco:
            if stringa in _.lower():
                j = int(i/10)
                self.IM = PIL.Image.open (_ + ".jpg")
                self.II = PIL.ImageTk.PhotoImage (self.IM)             
                self.button = Button(text = _, compound = 'top', image =  self.II, command = secondWindow).grid(row = j, column = i-j*10)
                i += 1

    def mainEnter (self):

        testoEntry = StringVar()      
        self.mainEntry = Entry(self.mainWindow, textvariable = testoEntry).place (x = 900, y = 20)

        def search ():
            testoEntry2 = testoEntry.get()
            if testoEntry2 == "":
                pass
            else:
                testoEntry2 = testoEntry2.lower()
            mainWindow.Buttons(self, testoEntry2)

        self.button4Entry = Button (self.mainWindow, text = 'search', command = search).place (x = 1050, y = 17)


MW = mainWindow()
MW.mainEnter()
mainloop()

如果我尝试在没有图像的循环中创建按钮,它们会起作用:

def Buttons(self, stringa):

    i = 0

    for _ in elenco:
        if stringa in _.lower():
            j = int(i/10)
            self.button = Button(text = _, command = secondWindow).grid(row = j, column = i-j*10)
            i += 1

如果我尝试创建一个带有图像但不在循环中的按钮,它也可以:

im = PIL.Image.open("immagine1.jpg")
ge = PIL.ImageTk.PhotoImage (im)
butt = Button(text = 'immagine', compound = 'top', image = ge, command = secondWindow).grid(row = 0, column = 0)

【问题讨论】:

  • 您只保留对 last 图像和最后一个按钮的引用。请改用列表 (self.buttons.append(...))。
  • @jonrsharpe 我用字典试过了,但它不起作用,我会用一个列表试试...
  • 此外,您存储的是.grid 的结果,实际上是None,而不是按钮本身。
  • 我也尝试过使用列表...而且我不知道该怎么做...也许这是一件愚蠢的事情,但我刚刚开始使用 Tkinter 和我'我不是 Python 专家
  • 你已经发布了你的旧代码,我已经告诉你为什么那行不通了;您需要保留对所有图像的引用。请提供minimal reproducible example 说明您尝试使用列表解决此问题。

标签: image button tkinter python-3.3


【解决方案1】:

假设您有名为“image-i.png”的图片,i=0,..,9。 当我执行以下代码(使用 python 3.5)时,我获得了十个带有图像和文本的工作按钮:

from tkinter import Tk, Button, PhotoImage

root = Tk()

images = []
buttons = []

for  i in range(10):
    im = PhotoImage(master=root, file="image-%i.png" % i)

    b = Button(root, text="Button %i" % i, image=im, compound="left", 
               command=lambda x=i: print(x))
    b.grid(row=i)

    images.append(im)
    buttons.append(b)

root.mainloop()

【讨论】:

  • 啊,好吧...我会尝试以这种方式编码...我的错误是我只用按钮或只有图像制作列表...忘记做一件事和另一件事...谢谢
猜你喜欢
  • 1970-01-01
  • 2018-04-25
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多