单击按钮后调用函数后发生错误

时间:2019-04-18 03:38:49

标签: python python-3.x tkinter

有人可以帮我为什么在尝试运行代码时为什么会出错,这是在我致电gui_alltime_music()时发生的。

错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Sujals\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/Sujals/PycharmProjects/ITmp3/GUI.py", line 12, in gui_alltime_music`enter code here'
    Label(guiforalltime, image=logo_all, bg='black').grid(row=1, column=0, sticky=W)
  File "C:\Users\Sujals\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2766, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\Sujals\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage2" doesn't exist

这主要发生在第12行中

Label(guiforalltime, image=logo_all, bg='black').grid(row=1, column=0, sticky=W)

代码:

from tkinter import *
from PIL import Image, ImageTk


#All time music GUI

def gui_alltime_music():
    # Main Gui

    guiforalltime = Tk()
    guiforalltime.title("All TIME MUSIC")
    guiforalltime.configure(background='black')
    # Labels

    # Logo for the GUI

    logo_all = PhotoImage(file='alltime.gif')
    Label(guiforalltime, image=logo_all, bg='black').grid(row=1,
          column=0, sticky=W)

    # Running gui forever
    guiforalltime.mainloop()


#Main Gui
gui= Tk()
gui.title("ITmp3")
gui.configure(background="Black")

# Images
logo = PhotoImage(file ='me.gif')
Label (gui, image=logo, bg="black") .grid(row=0, column=0, sticky=W)

#Labels
Label(gui, text="Everyone Loves Music", bg="black", fg="white", 
      font="none 12 bold").grid(row=1, column=0,sticky=W)

#Buttons
Button(gui, text="Best of All Time", command=gui_alltime_music, 
      bg="purple", fg="white", font="none 12 bold").grid(row=2,column=0,sticky=W)


#run the main loop
gui.mainloop()
  

“我希望它显示一个带有我喜欢的图像的窗口,而不是它   给了我一个随机错误。”

     

“我已经在目录中保存了所有照片,因此不是   问题”

1 个答案:

答案 0 :(得分:0)

我运行了您的代码,但遇到了同样的错误。问题是您已经有一个Tk作为主窗口,打开另一个主窗口会造成问题。因此,每当需要另一个窗口时,我们都使用 modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b => { b.Property<string>("LoginProvider") .HasMaxLength(256); b.Property<string>("ProviderKey") .HasMaxLength(256); b.Property<string>("ProviderDisplayName"); b.Property<string>("UserId") .HasMaxLength(256) .IsRequired(); b.HasKey("LoginProvider", "ProviderKey"); b.HasIndex("UserId"); b.ToTable("AspNetUserLogins"); }); 。同样,也不需要Toplevel(),因为已经有guiforalltime.mainloop()在运行以保持窗口打开。

更改

gui.mainloop()Tk(),并在函数Toplevel()中删除guiforalltime.mainloop()

完整代码

gui_alltime_music()