首页 > 解决方案 > Python:第二个窗口中的图片

问题描述

当我尝试在第二个窗口中插入背景图片时,我总是收到一条错误消息。如果我将它插入到我的第一个窗口中,我不会收到此错误消息。

from tkinter import *

tkFenster1 = Tk()
tkFenster1.title('LoveResult')
tkFenster1.geometry('300x300')
tkFenster1.configure(bg='')

schalke = PhotoImage(file='hallo.gif')
my_canvasschalke = Canvas(master=tkFenster1, width=300, height=300)
my_canvasschalke.pack(fill='both', expand=True)
my_canvasschalke.create_image(0,0, image=schalke, anchor='nw')
Exception in Tkinter callback
  Traceback (most recent call last):
  
  File "<ipython-input-1-24d984b0ec0f>", line 62, in buttonCalculateClick
    schalke = PhotoImage(file='hallo.gif')

  File "C:\Users\tjark\anaconda3\lib\tkinter\__init__.py", line 4061, in __init__

Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\tjark\anaconda3\lib\tkinter\__init__.py", line 4006, in __init__

    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "hallo.gif": no such file or directory

标签: pythontkinter

解决方案


嗨,如果你想制作子窗口,你可以使用一次 Tk(),如果你正在使用函数,你应该在子窗口函数部分中将背景设为全局,就这样,背景图像将显示在子窗口上


推荐阅读