Python 按钮图像未出现在tkinter中

Python 按钮图像未出现在tkinter中,python,tkinter,osx-yosemite,Python,Tkinter,Osx Yosemite,我是tkinter新手,在Yosemite下使用IDLE 3.4.2运行Python3.4.2,Tk版本为8.5.17。这是Liang在《使用Python 3编程入门》一书中修改的演示。我希望下面的代码显示四个按钮: from tkinter import * class ButtonsDemo: def __init__(self): window = Tk() window.title("Buttons Demo") frame0

我是tkinter新手,在Yosemite下使用IDLE 3.4.2运行Python3.4.2,Tk版本为8.5.17。这是Liang在《使用Python 3编程入门》一书中修改的演示。我希望下面的代码显示四个按钮:

from tkinter import *

class ButtonsDemo:
    def __init__(self):
        window = Tk()
        window.title("Buttons Demo")

        frame0 = Frame(window)  
        frame0.pack()

        plusImage = PhotoImage(file = "image/plus.gif")
        minusImage = PhotoImage(file = "image/minus.gif")
        timesImage = PhotoImage(file = "image/times.gif")
        divideImage = PhotoImage(file = "image/divide.gif")

        Button(frame0, image = plusImage, command =
                 self.add).grid(row = 1, column = 1, sticky = W)
        Button(frame0, image = minusImage, command =
                 self.subtract).grid(row = 1, column =2)
        Button(frame0, image = timesImage, command =
                 self.multiply).grid(row = 1, column = 3)
        Button(frame0, image = divideImage, command =
                 self.divide).grid(row = 1, column = 4)
        window.mainloop()

    def add(self):
         print("add pressed")

    def subtract(self):
         print("subtract pressed")

    def multiply(self):
         print("multiply pressed")

    def divide(self):
         print("divide pressed")

ButtonsDemo()
当我运行代码时,只显示前三个按钮。带divideImage的按钮不会出现

如果我单击divide按钮应该在哪里,我确实会收到divide被按下的消息。好像按钮在那儿,但它是看不见的

我在命令行中看到了同样的行为 而不是从空闲开始

gif没有问题。如果我将divide.gif用于plusImage,我可以在第一个按钮位置看到divide.gif。另外,当我将text=Divide而不是image=DivideImage放在Divide按钮上时,按钮变为可见

为什么使用图像时“分割”按钮不可见?我怎样才能修好它

编辑:这是代码,所有内容都移动到主程序。我认为这应该已经解决了垃圾收集问题,如果有的话。这是正确的吗

from tkinter import *

def add():
         print("add pressed")

def subtract():
     print("subtract pressed")

def multiply():
     print("multiply pressed")

def divide():
     print("divide pressed")


window = Tk()
window.title("Buttons Demo")
frame0 = Frame(window)  
frame0.pack()

plusImage = PhotoImage(file = "image/plus.gif")
minusImage = PhotoImage(file = "image/minus.gif")
timesImage = PhotoImage(file = "image/times.gif")
divideImage = PhotoImage(file = "image/divide.gif")

Button(frame0, image = plusImage, command =
             add).grid(row = 0, column = 0, sticky = W)
Button(frame0, image = minusImage, command =
             subtract).grid(row = 0, column = 1)
Button(frame0, image = timesImage, command =
             multiply).grid(row = 0, column = 2)

Button(frame0, image = divideImage, command =
             divide).grid(row = 0, column = 3)

window.mainloop()

您的代码对我来说非常有效,但我确实注意到您将divide按钮放在第5列而不是第4列。这里也一样。显示图像没有问题。实际上,您的列应该为零索引感谢反馈。将divide按钮移动到第4列并没有什么不同。第四个按钮对我来说仍然是看不见的。Rob和Marcin,你用什么平台运行代码?我的问题可能是因为我在使用约塞米蒂?