通过按钮打开另一个窗口并使用Tkinter、Python返回空窗口

通过按钮打开另一个窗口并使用Tkinter、Python返回空窗口,python,button,tkinter,pygame,mp3,Python,Button,Tkinter,Pygame,Mp3,当通过单击另一个按钮打开另一个窗口时,网格()无法绘制按钮,只返回一个空窗口和一个错误: _tkinter.TclError: image "pyimage1" doesn't exist 我在另一个主题()中读到了类似的问题,但它发生在一个标签上,它是使用.draw()解决的。当小部件是按钮时,我想知道是否有类似的解决方案 PS:我确信我从项目文件夹中正确地放置了图像路径。运行程序时,第一个按钮上可以显示相同的图像 从tkinter导入* 导入pygame root=Tk

当通过单击另一个
按钮打开另一个窗口时,
网格()
无法绘制
按钮
,只返回一个空窗口和一个错误:

_tkinter.TclError: image "pyimage1" doesn't exist
我在另一个主题()中读到了类似的问题,但它发生在一个
标签上,它是使用
.draw()
解决的。当小部件是
按钮时,我想知道是否有类似的解决方案
PS:我确信我从项目文件夹中正确地放置了图像路径。运行程序时,第一个按钮上可以显示相同的图像

从tkinter导入*
导入pygame
root=Tk()
root.title('Test')
def open_window():
其他窗口=Tk()
def播放声音():
pygame.init()
pygame.mixer.music.load('path.mp3')
pygame.mixer.music.play()
pygame.event.wait()
bt_play_sound=按钮(其他_窗口,文本='play',图像=img_play,命令=play_sound,composite=“top”)
bt_play_sound.grid(行=0,列=0)
其他_window.mainloop()
#图像
img_play=PhotoImage(file=“path.png”)
打开其他窗口=按钮(root,text=“打开”,image=img\u播放,command=open\u窗口,component=“顶部”)
打开其他窗口网格(行=0,列=0)
root.mainloop()

要使第二个图像正常工作,您需要创建一个新的PhotoImage,并将
master
选项设置为第二个Tk窗口

def open_window():
    other_window = Tk()

    def play_sound():
        pygame.init()
        pygame.mixer.music.load('path.mp3')
        pygame.mixer.music.play()
        pygame.event.wait()
        
    img_play = PhotoImage(file="path.png", master=other_window)    # <<<< Add this line
    bt_play_sound = Button(other_window, text = 'Play', image=img_play, command = play_sound, compound="top")
    bt_play_sound.grid(row = 0, column = 0)
    
    other_window.mainloop()
def open_window():
其他窗口=Tk()
def播放声音():
pygame.init()
pygame.mixer.music.load('path.mp3')
pygame.mixer.music.play()
pygame.event.wait()

img_play=PhotoImage(file=“path.png”,master=other_window)#不建议使用多个
Tk()
将子窗口替换为
Toplevel()