Wie kann ich eine Python -Desktop -Anwendung erstellen, die transparent ist und Freihandschaltung ermöglichtPython

Python-Programme
Anonymous
 Wie kann ich eine Python -Desktop -Anwendung erstellen, die transparent ist und Freihandschaltung ermöglicht

Post by Anonymous »

Ich habe den folgenden Code ausprobiert, aber er hat zu einem unsichtbaren Fenster geführt, in dem Mausereignisse nicht erkannt werden, sodass ich nicht darauf zeichnen kann. FYI Ich bin auf einem Windows 11 -Computer. < /P>
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("", self.paint)
self.canvas.bind("", 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()
< /code>
Es ist unbedingt erforderlich, dass ich eine transparente Hintergrundanwendung habe, die setzbar ist und darauf zurückgreifen kann. Ich muss auch vordefinierte Farben haben, aus denen ich für das Stiftwerkzeug und eine "klare" Taste auswählen kann, um alle Zeichnungen zu löschen. Kann jemand bitte helfen, wo ich falsch gehe, und schlage irgendwelche Codesänderungen vor?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post