0

A difficult task has arisen, let's imagine a situation, I have a computer game and I want to let's say... draw your cursor using python, or some shapes. It doesn't matter why I need it, I just need it and that's it =) The first thing I tried to use was Tkinter. The plan was to create a transparent gui window on top of the game window, in which I would use create_rectangle to draw the shapes I needed, but as I understand it, Tkinter does not know how to make windows transparent, because if you use attributes("-fullscreen", True, '-alpha', 0), then all the shapes also become transparent.. Moreover, I'm not sure if the gui has the ability to make a window in such a way that it does not interfere with clicks in the game window, i.e. in other words, is it possible to make the clicks go "through" the python GUI window. Therefore, I had another idea, using win32gui to draw pixels directly on the screen, but this method also did not suit me because it is an extremely slow process and even drawing 1 pixel in an endless loop makes windows slow down, not to mention that the pixel is constantly disappearing because of the game image on the screen updated.

Please tell me if it is possible to create a transparent window with the ability to display shapes on it through any gui libraries for python, and even so that this window does not interfere with mouse and keyboard clicks on the game or other programs, it does not matter. In other words, so that it is transparent both visually and for clicks. at the same time, it was on top of other windows.

from tkinter import *


root = Tk()
root.title("METANIT.COM")
root.attributes("-fullscreen", True, '-alpha', 0.3) #на весь экрн root


canvas = Canvas(root, bg='white', highlightthickness=0)
canvas.pack(fill=BOTH, expand=True) #canvas на весь root


root_width = root.winfo_screenwidth() #Ширина окна
root_height = root.winfo_screenheight() #Высота окна


center_root_width = root.winfo_screenwidth()/2 #Центр ширины
center_root_height = root.winfo_screenheight()/2 #Центр высоты

size = 2 # размер области для лучшей видимости

canvas.create_rectangle(center_root_width-size,center_root_height-size,center_root_width+size,center_root_height+size,outline='red') #Нарисовать пиксель в центре экрана



canvas.pack(anchor=CENTER, expand=1)
root.mainloop()

(If you set alpha = 0, not only the window becomes transparent, but also the elements on the window. There will also be problems with the python window interfering with clicking on the game window.)

and

import win32gui
import win32api

width = 1920
height = 1080

x = width/2
y = height/2


while True:

    dc = win32gui.GetDC(0)
    red = win32api.RGB(255, 0, 0)

    win32gui.SetPixel(dc, int(x), int(y), red)  # draw red at 0,0

(Slows down, flashes in the game, disappears)

I don't know how, and I don't want to interfere in any way with the game process, otherwise I risk getting banned. I just want to draw on my screen so that this image is displayed on top of the game window without interfering with my playing.

1

1 Answer 1

1

I think you can solve this with Tkinter in Python.

For the first problem, getting a transparent background, you can bypass this by setting an arbitrary alias name and then using this alias as background color :

root.wm_attributes("-transparentcolor", "yellow") 
cv = Canvas(root, bg="yellow")   # create a transparent canvas

With this, the clicks already go through the window when you click on something transparent. You just have to keep the window on topmost to have your overlay :

root.wm_attributes("-topmost", 1)  # make root foreground

Here is a minimal example :

from tkinter import Tk, Canvas

root = Tk()
# make root foreground
root.wm_attributes("-topmost", 1)
root.wm_attributes("-transparentcolor", "yellow")
# fill the window with transparent canvas
cv = Canvas(root, bg="yellow")
cv.create_rectangle(50, 50, 100, 100, fill="red")
cv.pack(expand=True, fill='both')

root.mainloop()

Does this work for your usage ?

Also you can check this answer, i's a bit more advanced and it seems similar to your case : https://stackoverflow.com/a/29854420/25935522

Sign up to request clarification or add additional context in comments.

1 Comment

While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.