【问题标题】:How do I get a PNG file to be the background of a window? RE: Python, tkinter, pillow, pil如何让 PNG 文件成为窗口的背景?回复:Python,tkinter,枕头,pil
【发布时间】:2022-01-18 04:04:58
【问题描述】:

目标:将“tomato.png”(左边的图片)作为右边窗口的背景。

当我运行代码时,窗口甚至不会出现,我会收到以下消息:

_tkinter.TclError:无法识别图像文件“tomato.png”中的数据

在之前的 tkinter 工作中,我使用了 tkk。为了让文字出现在我正在使用的按钮上,我在这里尝试了但无济于事(这种方法让我很兴奋。我从 12 月 31 日开始学习 Python ——还有很多我不知道。)

我用谷歌搜索了我的问题,并且对 Pil/Pillow 有一个粗略的了解……所以我通过 Pycharm 安装了 Pillow。

取得了一些进展。通过Pycharm安装Pillow后,窗口确实出现了,错误信息没有,但是...图像没有产生。

另外,这是我正在使用的 python 的路径:/Users/my_name/.pyenv/versions/3.10.0/bin/python

我将感谢您提供的所有帮助并牢记...我刚刚学习 Python 两周,具有金融背景-我不喜欢编程用语...根本。即使在研究这个问题时,也有人建议通过命令行安装 pip,坦率地说,我不知道发生了什么。

另外,这是我的第一篇文章,所以如果我没有遵循这篇文章中的 SOP,请不要把我钉在十字架上。你们有些人在回答问题时很无情。

感谢您的宝贵时间。

https://i.stack.imgur.com/BCehz.png 我的代码照片的链接应该在这里^^

但是我这里是手动输入的:

from tkinter import 

from tkinter import ttk

from PIL import Image

from PIL import ImageTk

# ---------------------------- CONSTANTS ------------------------------- #
PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
WORK_MIN = 25
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 20

# ---------------------------- TIMER RESET ------------------------------- # 

# ---------------------------- TIMER MECHANISM ------------------------------- # 

# ---------------------------- COUNTDOWN MECHANISM ------------------------------- # 

# ---------------------------- UI SETUP ------------------------------- #

window = Tk()
window.title("Pomodoro")

canvas = Canvas(width=200, height=224)
tomato_pic = Image.PhotoImage(file="tomato.png")
canvas.create_image(100, 112, image=tomato_pic)
canvas.pack()

window.mainloop()

【问题讨论】:

  • 我们看不到带有您的代码照片的链接。但是,如果您将问题中的代码粘贴为文本并用 ``` my code ``` 将其包围起来会更方便,这将给它一些语法突出显示。
  • 这真是一个烹饪技巧。谢谢@lane
  • Image.PhotoImage(file="tomato.png") 应该是ImageTk.PhotoImage(file="tomato.png")
  • 修复错字并使用我的 PNG 图像后无法重现该问题。
  • 我试过了,它不会产生图像,但我很欣赏这个建议!

标签: python tkinter python-imaging-library png


【解决方案1】:

我在这里找到了替代方案:

https://stackoverflow.com/a/46624625/8146119


    import Tkinter as tk
    from PIL import ImageTk, Image
    window = tk.Tk()
    window.title("tkinter stuff")
    window.geometry("300x300")
    window.configure(background='grey')
    path = "hs.gif"
    img = ImageTk.PhotoImage(Image.open(path))
    panel = tk.Label(window, image = img)
    panel.pack(side = "bottom", fill = "both", expand = "yes")
    window.mainloop()

我必须找出是什么让这段代码工作以及是什么让我的代码失败。 ## 更新 ## 问题是我没有使用适当的 tkinter,我使用的是 8.5,而我应该使用 8.6。我也在使用 python 3.10,因为 tkinter,基于我在网上的粗略搜索,在 3.9 上不起作用。我正在使用 3.8。我遇到的问题不再是。 希望有人会从中受益。感谢大家的时间。

【讨论】:

    【解决方案2】:

    首先,使用较新版本的 Python 可能会更好。各种 stackoverflow 海报已经注意到 tkinter 支持图像的问题。见帖子here

    根据this stackoverflow 的回答,tkinter.LabelLabel.place 是在 tkinter 窗口中放置背景图像的便捷技巧。以下是这如何适合您的代码。它适用于我使用 Python 3.8 和 Windows 10。

    from tkinter import *
    from PIL import Image
    from PIL import ImageTk
    
    # ---------------------------- CONSTANTS ------------------------------- #
    PINK = "#e2979c"
    RED = "#e7305b"
    GREEN = "#9bdeac"
    YELLOW = "#f7f5dd"
    FONT_NAME = "Courier"
    WORK_MIN = 25
    SHORT_BREAK_MIN = 5
    LONG_BREAK_MIN = 20
    
    # ---------------------------- TIMER RESET ------------------------------- # 
    
    # ---------------------------- TIMER MECHANISM ------------------------------- # 
    
    # ---------------------------- COUNTDOWN MECHANISM ------------------------------- # 
    
    # ---------------------------- UI SETUP ------------------------------- #
    
    window = Tk()
    window.title("Pomodoro")
    
    canvas = Canvas(width=200, height=224)
    tomato_pic = PhotoImage(file="tomato.png")
    # note here use of Label
    background_label = Label(window, image=tomato_pic)
    # label.place is tk trick that is good for backgrounds
    background_label.place(x=0, y=0, relwidth=1, relheight=1)
    canvas.pack()
    
    window.mainloop()
    

    然后您可能需要使用 relwidth、relheight 参数并可能缩小原始图像的大小以使其适合。

    【讨论】:

    • 谢谢!这就是我得到的Traceback (most recent call last): File "/Users/*/Documents/#100daysofCode/pomodoro-start/main.py", line 28, in <module> tomato_pic = PhotoImage(file="tomato.png") File "/Users/*/.pyenv/versions/3.10.0/lib/python3.10/tkinter/__init__.py", line 4093, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "/Users/*/.pyenv/versions/3.10.0/lib/python3.10/tkinter/__init__.py", line 4038, in __init__ self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't recognize data in image file "tomato.png"
    • 您的 png 文件可能存在问题。尝试从网上找到一个真正的 png。喜欢upload.wikimedia.org/wikipedia/commons/9/9d/Tomato.png
    • 我刚刚尝试过......但它也不起作用。我很感激你的时间。我会坚持下去的。非常感谢。
    • 好的,我的答案再更新一次。您可能需要更新 Python。请参阅另一个具有相同错误的帖子的链接
    • 我已经尝试在使用 Label 和 .place() 之前指定完整路径,我现在尝试了,得到了相同的“无法识别数据”错误。我在我的 pycharm 解释器和 tkinter 8.5.9 中使用 python3.10(通过自制软件)。
    猜你喜欢
    • 1970-01-01
    • 2021-03-20
    • 2011-10-13
    • 2021-11-20
    • 2021-03-12
    • 1970-01-01
    • 2014-05-08
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多