当我尝试在第二个窗口中插入背景图片时,总是收到错误消息。如果我将其插入到第一个窗口中,我不会收到此错误消息。
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
您只需使用
Tk()
一次,如果您想创建子窗口,则应该使用Toplevel()
,如果您正在使用函数,则应在子窗口功能部分中将背景设置为全局,仅此而已,背景图像将显示在子窗口上