Ian
Ian

Reputation: 9

Is it possible to save a tkinter window widget as an image?

I am trying to use the python tkinter library to create an image from a window widget. I have seen several methods that allow you to use a canvas widget to save an image, but I don't want to use a canvas as I have already created the UI for the image I want to save. If anybody has any better ways to do this through python, I am trying to create an image using some data from the Notion API that I can then send to an e-ink display through a Raspberry Pi. I am still relatively new to python, so any and all help is appreciated!

Here is my code if it helps:

import tkinter as tk
from PIL import Image


def renderDisplay(notStartedCards, inProgressCards):
    window = tk.Tk()
    window.geometry("800x480")

    notStarted = tk.Frame(window, borderwidth=1, relief="solid", width=275, height=480)
    notStarted.pack(side="left")
    notStarted.pack_propagate(False)

    notStartedLabel = tk.Label(notStarted, text="Not Started", font=("Arial", 15))
    notStartedLabel.pack(side="top", pady=5)

    fillCards(notStartedCards, notStarted)

    inProgress = tk.Frame(window, borderwidth=1, relief="solid", width=275, height=480)
    inProgress.pack(side="left")
    inProgress.pack_propagate(False)

    inProgressLabel = tk.Label(inProgress, text="In Progress", font=("Arial", 15))
    inProgressLabel.pack(side="top", pady=5)

    fillCards(inProgressCards, inProgress)

    agenda = tk.Frame(window, height=480, borderwidth=1, relief="solid")
    agenda.pack(fill="x")

    window.mainloop()


def fillCards(cards, parent):
    count = 0
    for item in cards:
        cardFrame = tk.Frame(parent, width=225, height=80, borderwidth=2, relief="solid")
        cardFrame.pack_propagate(False)

        name = tk.Label(cardFrame, text=item["name"], font=("Arial", 12))
        name.pack(anchor="nw")

        priority = tk.Label(cardFrame, text=item["priority"])
        priority.pack(anchor="nw", pady=3)

        due = tk.Label(cardFrame, text=item["due"])
        due.pack(anchor="nw")

        cardFrame.pack(side="top", pady=4, padx=15)
        count += 1
        if count == 5:
            break

Upvotes: 0

Views: 108

Answers (0)

Related Questions