[Python] How can I create a python desktop application that is transparent and allows freehand...

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have tried the below code but it has resulted in an invisible window that does not recognise mouse events so I cannot draw on it. FYI I am on a windows 11 computer.

    import tkinter as tk
    from tkinter import Button
    from PIL import Image, ImageDraw
    import win32api
    import win32con
    import win32gui

    class TransparentDrawingApp:
    def __init__(self, root):
    self.root = root
    self.root.title("Transparent Drawing App")

    # Set window size and make it resizable
    self.root.geometry("800x600")
    self.root.resizable(True, True)

    # Create a canvas for drawing (white background initially)
    self.canvas = tk.Canvas(self.root, width=800, height=600, bg="white", bd=0, highlightthickness=0)
    self.canvas.pack(fill=tk.BOTH, expand=True)

    # Make the window transparent by using white as the transparent color
    self.root.configure(bg='white')
    self.root.attributes("-transparentcolor", "white") # Make white the transparent color
    self.root.wm_attributes("-topmost", 1) # Keep window always on top

    # Initialize the drawing image with transparent background
    self.image = Image.new("RGBA", (800, 600), (255, 255, 255, 0)) # Transparent background
    self.draw = ImageDraw.Draw(self.image)

    # To store the last mouse position for drawing
    self.last_x, self.last_y = None, None

    # Predefined colors
    self.colors = ["red", "green", "blue", "black", "yellow", "purple"]
    self.current_color = "black"

    # Add color buttons to change drawing color
    self.create_color_buttons()

    # Add a "Clear" button to erase drawings
    self.clear_button = Button(self.root, text="Clear", command=self.clear_canvas)
    self.clear_button.pack(pady=10)

    # Bind mouse events for drawing
    self.canvas.bind("<B1-Motion>", self.paint)
    self.canvas.bind("<ButtonRelease-1>", self.reset)

    # Make the window fully transparent using win32gui
    self.make_window_transparent()

    def create_color_buttons(self):
    """Create color buttons to change drawing color."""
    color_frame = tk.Frame(self.root)
    color_frame.pack(pady=10)

    for color in self.colors:
    button = Button(color_frame, bg=color, width=2, height=1, command=lambda c=color: self.set_color(c))
    button.pack(side=tk.LEFT, padx=5)

    def set_color(self, color):
    """Change the drawing color."""
    self.current_color = color

    def paint(self, event):
    """Handles the drawing functionality."""
    x, y = event.x, event.y
    if self.last_x and self.last_y:
    # Draw on the canvas
    self.canvas.create_line(self.last_x, self.last_y, x, y, width=2, fill=self.current_color, capstyle=tk.ROUND, smooth=True)
    # Also draw on the image to preserve drawing
    self.draw.line([self.last_x, self.last_y, x, y], fill=self.current_color, width=2)
    self.last_x, self.last_y = x, y

    def reset(self, event):
    """Resets the mouse position."""
    self.last_x, self.last_y = None, None

    def clear_canvas(self):
    """Clears the canvas."""
    self.canvas.delete("all")
    self.image = Image.new("RGBA", (800, 600), (255, 255, 255, 0)) # Reset the image to transparent
    self.draw = ImageDraw.Draw(self.image)

    def make_window_transparent(self):
    """Make the window fully transparent using win32gui."""
    hwnd = win32gui.GetForegroundWindow()
    win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, win32con.WS_EX_LAYERED | win32con.WS_EX_TOPMOST)
    win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(255, 255, 255), 0, win32con.LWA_COLORKEY)


    if __name__ == "__main__":
    root = tk.Tk()
    app = TransparentDrawingApp(root)
    root.mainloop()


    It is imperative that I have a transparent background application that is resizable and can draw on. I also need to have pre-defined colors that I can choose from for the pen tool and a "clear" button to clear all of the drawings. Can someone please assist in where I am going wrong and suggest any code amendments?

    Continue reading...

Compartilhe esta Página