简体   繁体   English

(Python-Tkinter)函数执行期间,将图像用作第二个窗口背景

[英](Python - Tkinter) During a function, using an image as second window background

So I've looked around at forums and video tutorials but I can't seem to solve the problem I'm having. 因此,我四处浏览论坛和视频教程,但似乎无法解决自己遇到的问题。 Basically, I'm creating a GUI with multiple windows. 基本上,我正在创建具有多个窗口的GUI。 When trying to set up my second window, I attempt to set an image as the background using the following code; 尝试设置第二个窗口时,我尝试使用以下代码将图像设置为背景;

def openMusicPage():                                                               1
    MusicPage = Tk()                                                               2
    MusicPage.title('Now This Is Epic: Top Music Chart')                           3
    MusicPage.configure(width = 800, height = 505)                                 4

    # Background`enter code here`
    MusicBackground = PhotoImage(file="MusicBackground.gif")                       5   
    MusicBackground_label = Label(MusicPage, image = MusicBackground)              6
    MusicBackground_label.image=MusicBackground                                    7
    MusicBackground_label.place(x=0, y=0)                                          8

The error occurs on the 6th line saying the image doesn't exist. 错误发生在第6行,表示该图像不存在。 On other forums and videos, I've seen suggestions to add the 7th line to fix this issue by referencing the image again. 在其他论坛和视频上,我看到了建议添加第七行以通过再次引用该图像来解决此问题。 However, this hasn't helped and I continue to receive the error. 但是,这没有帮助,我继续收到该错误。 Any suggestions as to how I could solve this issue? 关于如何解决此问题有什么建议吗? Noting, I am only able to use Tkinter for this assignment. 请注意,我只能使用Tkinter进行此作业。

Thanks in advance! 提前致谢!

Only use one instance of Tk() . 仅使用Tk()一个实例。 For additional windows use Toplevel() . 对于其他窗口,请使用Toplevel() Change that and it works just fine. 更改它,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM