Я попробовал приведенный ниже код, но он привел к невидимому окну, которое не распознает события мыши, поэтому я не могу опираться на него. К вашему сведению, я на компьютере Windows 11. < /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>
Крайне важно, чтобы у меня было прозрачное фоновое приложение, которое можно было бы использовать и нарисовать. Мне также нужно иметь предварительно определенные цвета, из которых я могу выбрать для инструмента для ручки, и кнопку «Чистые», чтобы очистить все чертежи. Может ли кто -нибудь помочь в том, где я ошибаюсь, и предложить какие -либо поправки кода?
Подробнее здесь: https://stackoverflow.com/questions/795 ... allows-fre
Как я могу создать прозрачное настольное приложение Python и позволяет рисовать от руки. ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как позволить пользователю рисовать пальцами и рисовать геометрические фигуры на Android?
Anonymous » » в форуме Android - 0 Ответы
- 33 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как преобразовать настольное приложение Python Tkinter в приложение Kivy Android
Anonymous » » в форуме Python - 0 Ответы
- 3 Просмотры
-
Последнее сообщение Anonymous
-