1

I have this program that guess the hexadecimal number of the colour pointed when "p" is pressed and it works, as you can see trying my code if you click out of the program area the program remains on top level and doesn't get covered by other programs, but the program loses the focus so in order to use the "p" function i have to click to the program and than press "p", do you know how to keep alway the program on focus even when i click out of the program area? Thanks a lot

from tkinter import *
import pyautogui
import pyperclip
import PIL.ImageGrab
from PIL import Image,ImageTk
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
b,x,y = "","",""
def cattura_colore(event):
    global etichetta_valore
    global b
    global x
    global y

    try:
        a = pyautogui.position()
        a = str(a)
        a = a.replace("Point","")
        a = a.replace("(", "")
        a = a.replace(")", "")
        a = a.replace("x","")
        a = a.replace("y", "")
        a = a.replace("=", "")
        b = a
        x = a.split(",")[0]
        y = a.split(",")[-1]
        y = y.replace(" ","")
        x,y = int(x),int(y)
        print(x,y)
        rgb = PIL.ImageGrab.grab().load()[x, y]
        rgb = '#%02x%02x%02x' % (rgb)
        rgb = rgb.upper()
        print(rgb)
        etichetta_valore["text"] = rgb

    except(IndexError):
        pass
    except(NameError):
        pass
window = Tk()
window.title("Valore esadecimale")
window.geometry("500x250")
window.resizable(width=False, height=False)
window.configure(bg="#292929")
window.attributes('-topmost', 1)
window.bind("<p>",cattura_colore)
etichetta_titolo_valore = Label(window, text="Valore:", font=("Ink Free", 20), bg="#292929", fg="white")
etichetta_titolo_valore.pack(pady=(20, 0))
etichetta_valore = Label(window, text="", font=("Ink Free", 20), bg="#292929", fg="white")
etichetta_valore.pack(pady=(10, 0))
window.mainloop()
  • window.grab_set_global() "Widget w grabs all events for the entire screen. This is considered impolite and should be used only in great need. Any other grab in force goes away. Try to use this awesome power only for the forces of good, and never for the forces of evil, okay?" – Сергей Кох Jun 07 '22 at 12:25
  • Hi @СергейКох thanks for the answer, i tried but it doesn't work because if i click out of the program the tkinter window is still not in focus, any way to always keep the program window in focus even when i click out of the progam area? – Forty966699669 Jun 07 '22 at 12:57

0 Answers0