python - 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
解决方案
嗨,如果你想制作子窗口,你可以使用一次 Tk(),如果你正在使用函数,你应该在子窗口函数部分中将背景设为全局,就这样,背景图像将显示在子窗口上
推荐阅读
- cryptography - 对于不同的密钥和消息对,HMAC 输出是否相同
- apache-spark - 在通过 spark 结构化流写入文件时读取文件
- json - jq - 如何在单个数组中选择嵌套对象
- azure - Azure 事件网格:查明是否正在覆盖 blob
- jquery - XMLHTTP 请求不解析 XML 文件
- assembly - 为 8086 组装游戏添加背景音乐
- postgresql - 无法从另一个容器连接到 Postgres 容器
- javascript - 拖放后无法访问元素
- python - Dash 应用程序部署在 Heroku 上,但 * 出现 *(不完全确定)无法正确读取 .csv 文件
- audio - 如何在使用 pyav、ffmpeg、python 解码 mp3 文件后使用音频帧