0

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
Ian
  • 9
  • 1
  • 1
    Use `ImageGrab` from `Pillow` module to take a screenshot of the window and save it to an image. – acw1668 Mar 02 '23 at 15:15
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Blue Robin Mar 02 '23 at 16:20
  • Find a solution [here](https://stackoverflow.com/a/19964474/12621346) – Hermann12 Mar 02 '23 at 18:18
  • Will `ImageGrab` work if I'm using a Raspberry pi with no display attached? I am only communicating with my display through SPI, so I don't have the option to screenshot it. – Ian Mar 02 '23 at 18:19
  • Use a X server for a headless server such as Xvfb. See [this question](https://unix.stackexchange.com/questions/365268/how-do-i-take-a-screen-shot-of-my-xvfb-buffer). – relent95 Mar 03 '23 at 02:37

0 Answers0