0

i have been trying to create a python program similar to desktop goose in the aspect of a widget moving freely around the screen.

First i tried to create a window with tkinter and make a transparent window with a PNG picture of a character and then moving the window with win32gui or any other library that would allow me to do this. But first, the tkinter transparent window thing doesn't work because the widgets inherit the transparency, so there is no way i can display the PNG. Then i had trouble finding any win32gui function that would allow me to move the windows, i just found stuff that would let me resize them.

Is there any way i can do any of these two tasks?

  • 1
    Refer to this post for moving the window - https://stackoverflow.com/questions/31797063/how-to-move-the-entire-window-to-a-place-on-the-screen-tkinter-python3 – The Laggy Tablet Oct 15 '20 at 03:19

1 Answers1

1

You can create a transparent window using a transparent PNG image as below:

import tkinter as tk

# select a color as the transparent color
TRNAS_COLOR = '#abcdef'

root = tk.Tk()
root.overrideredirect(1)
root.attributes('-transparentcolor', TRNAS_COLOR)

image = tk.PhotoImage(file='/path/to/image.png')
tk.Label(root, image=image, bg=TRNAS_COLOR).pack()

# support dragging window

def start_drag(event):
    global dx, dy
    dx, dy = event.x, event.y

def drag_window(event):
    root.geometry(f'+{event.x_root-dx}+{event.y_root-dy}')

root.bind('<Button-1>', start_drag)
root.bind('<B1-Motion>', drag_window)

root.mainloop()

Then you can use root.geometry(...) to move the root window.

acw1668
  • 40,144
  • 5
  • 22
  • 34
  • it says "couldn't recognize data in image 'file.png'" i don't know if the problem is in the file or in the code, let me check with another image. –  Oct 15 '20 at 04:48
  • Try using `Pillow` module: `image = ImageTk.PhotoImage(file='/path/to/image.png')`. – acw1668 Oct 15 '20 at 04:50
  • Do you know how can i make it dragable? –  Oct 15 '20 at 21:45