TKinter按钮图像未显示

时间:2018-12-07 07:27:19

标签: python tkinter tkinter-canvas

我想让我的应用看起来更漂亮,而且比我更难!现在,我有了一个主要有2个按钮和顶部日期时间标签的应用程序。按钮只是带有文本,但是我想在它们上使用图片。现在是我的代码:

def showMainMenu():
    global cardNumber, allowToGPIO
    cardNumber = "" #Reset cardNumber global value
    allowToGPIO = True
    clear()
    showNewTime()
    watchDog.awake()
    GuestTestButton = Button(canvas, text="GUEST TEST", width=buttonwidth, height=buttonheight, compound = TKinter.CENTER, command = guestTest)
    GuestTestButton.config(font=("Mojave-Regular.ttf", 24))
    #GuestTestButton.pack()
    GuestTestButton.place(x=90, y=100)
    AddEmployeeButton = Button(canvas, text="ADD NEW USER", width=buttonwidth, height=buttonheight, compound = TKinter.CENTER, command = addEmployee)
    AddEmployeeButton.config(font=("Mojave-Regular.ttf", 24))
    #AddEmployeeButton.pack()
    AddEmployeeButton.place(x=90, y=270)

现在,它可以工作了。但是,当我尝试使它们更加鲜艳(仅使用图像而不是文本)时,按钮上没有任何显示。甚至可以在TKinter中制作类似的东西吗?我所做的一切都在画布上:

app = TKinter.Tk()
canvas = TKinter.Canvas()

我试图那样做:

GuestImage = TKinter.PhotoImage(file="guest.gif")
GuestTestButton = Button(canvas, text="GUEST TEST", width=buttonwidth, height=buttonheight, compound = TKinter.CENTER, command = guestTest)
GuestTestButton.config(image=GuestImage, font=("Mojave-Regular.ttf", 24))
GuestTestButton.place(x=90, y=100)

但是正如我所说,它无法正常运行:D预先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我现在知道了,我的图像出现了,但是在几毫秒后消失了,现在我将GuestImage和AddEmployeeImage设置为全局变量,并且效果很好!