【问题标题】:How to open a PIL image on every tkinter button press?如何在每次按下 tkinter 按钮时打开 PIL 图像?
【发布时间】:2020-01-28 10:51:42
【问题描述】:

我对 Pyton 和一般编程非常陌生,对于我的第一个项目,我正在尝试使用 Tkinter 界面制作一个脚本,该脚本应该执行以下操作:

  1. 允许用户在条目中写入一些文本,
  2. 在按钮单击时将该文本放置在具有已定义名称的图像上,
  3. 以当前日期和时间的名称保存该图像,
  4. 在单击按钮时重复上述所有操作。

该脚本仅能正常工作一次(在单击第一个按钮后),然后它将输入的文本放在先前输入的文本上。

我认为这是因为它只打开一次初始图像,并且每次单击按钮时都会执行其他步骤,但我似乎无法将定义初始图像的代码写入 command=lambda(它会导致各种错误) . 这是我的代码:

import tkinter as tk
from PIL import Image, ImageDraw, ImageFont
from datetime import datetime

def adresat1_function(self): draw.text(xy=(273, 215), text=(entry_1.get()),
    fill=(0, 0, 0), font=font_type)

root = tk.Tk()
root.title("Postal")
root.maxsize(height=530, width=590,)

canvas = tk.Canvas(root, height=530, width=590, highlightthickness=0)
canvas.pack()

frame_1 = tk.Frame(canvas, bg='#75a3a3', bd=2)
frame_1.place(x=5, y=10, height=200, width=580, anchor='nw')

entry_1 = tk.Entry(frame_1, font=18,)
entry_1.place(x=202, y=0, width=374, height=22,)

#Image

image = Image.open('Blank.jpg')
font_type = ImageFont.truetype('arial.ttf',14,)
draw = ImageDraw.Draw(image)

#Button

button = tk.Button(frame_1, text = 'Fill', width=8,
    command=lambda:

    #Fill
    (adresat1_function(entry_1.get()),
    image.save(datetime.now().strftime("%Y-%m-%d %H-%M-%S") + '.jpg'),))

button.place(x=202, y=160, width=374, height=22,)
root.mainloop()

要使这个程序将输入的文本保存在新图像上而不在先前输入的文本之上重写它,必须做些什么? 我什至可能不知道一些 Python 核心概念,在这种情况下,我很抱歉这个愚蠢的问题。提前致谢。

【问题讨论】:

    标签: python image button tkinter python-imaging-library


    【解决方案1】:

    修改您的代码如下所示,将解决问题。您想要的是,每次按下按钮时,都会创建一个新的图像副本,您可以在那里应用您的文本:

    import tkinter as tk
    from PIL import Image, ImageDraw, ImageFont
    from datetime import datetime
    
    # Changes here.
    def adresat1_function(self):
        newImage = image.copy()
        draw = ImageDraw.Draw(newImage)
        draw.text(xy=(273, 215), text=(entry_1.get()),fill=(0, 0, 0), font=font_type)
        newImage.save(datetime.now().strftime("%Y-%m-%d %H-%M-%S") + '.jpg')
    
    root = tk.Tk()
    root.title("Postal")
    root.maxsize(height=530, width=590,)
    
    canvas = tk.Canvas(root, height=530, width=590, highlightthickness=0)
    canvas.pack()
    
    frame_1 = tk.Frame(canvas, bg='#75a3a3', bd=2)
    frame_1.place(x=5, y=10, height=200, width=580, anchor='nw')
    
    entry_1 = tk.Entry(frame_1, font=18,)
    entry_1.place(x=202, y=0, width=374, height=22,)
    
    #Image
    
    image = Image.open('Blank.jpg')
    font_type = ImageFont.truetype('arial.ttf',14,)
    newImage = None
    
    #Button
    
    button = tk.Button(frame_1, text = 'Fill', width=8,
        command=lambda:
    
        # Changes in fill.
        (adresat1_function(entry_1.get()),))
    
    
    button.place(x=202, y=160, width=374, height=22,)
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 2013-08-24
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      相关资源
      最近更新 更多