D
D
dipperok2020-05-28 20:05:18
Python
dipperok, 2020-05-28 20:05:18

How to make a bind that works outside of a tkinter application?

I have the rawest version of my application for working out the bind mechanism.
The application has input lines (entry), "copy" and "clear" buttons, the "clear" button clears the input field, and the "copy" button copies the contents of the line to the clipboard using the "pyperclip" library. For example "pyperclip.copy(a)", where "a" is a string variable.
What I need is for me to be in the game, the browser (outside the application window itself, in simple terms) press the "k" button and execute a function that reads what the user entered into the entry and copies it to the clipboard.

def Copy1():
    ai = ent1.get()
    pyperclip.copy(ai)

I still don’t really understand how this can be implemented, everyone advises modules like "keyboard", but no one can say specifically how I can make this bind if I'm in the game, or just the application window is minimized.
For convenience, the whole code is:
from tkinter import *
import pyperclip


def Clear1():
    v1.set("")
def Copy1():
    ai = ent1.get()
    pyperclip.copy(ai)

def Clear2():
    v2.set("")
def Copy2():
    ai = ent2.get()
    pyperclip.copy(ai)

def Clear3():
    v2.set("")
def Copy3():
    ai = ent3.get()
    pyperclip.copy(ai)


root = Tk()
root.geometry('455x280')
root.title("pp")
root.resizable(False, False)
root.configure(background="#E6F0D5")
#root.iconbitmap('9.ico')


v1 = StringVar()
ent1 = Entry(font=("Ubuntu", 19), textvariable=v1, justify=CENTER)
but1 = Button(root, text='Copy', font=("Ubuntu", 13), height=1, width=20, bg='#F8F8F8', command=Copy1)
but2 = Button(root, text='Clear', font=("Ubuntu", 13), height=1, width=9, bg='#F8F8F8', command=Clear1)

ent1.place(x=82, y=24)
but1.place(x=82, y=60)
but2.place(x=274, y=60)


v2 = StringVar()
ent2 = Entry(font=("Ubuntu", 19), textvariable=v2, justify=CENTER)
but3 = Button(root, text='Copy', font=("Ubuntu", 13), height=1, width=20, bg='#F8F8F8', command=Copy2)
but4 = Button(root, text='Clear', font=("Ubuntu", 13), height=1, width=9, bg='#F8F8F8', command=Clear2)

ent2.place(x=82, y=104)
but3.place(x=82, y=140)
but4.place(x=274, y=140)


v3 = StringVar()
ent3 = Entry(font=("Ubuntu", 19), textvariable=v3, justify=CENTER)
but5 = Button(root, text='Copy', font=("Ubuntu", 13), height=1, width=20, bg='#F8F8F8', command=Copy3)
but6 = Button(root, text='Clear', font=("Ubuntu", 13), height=1, width=9, bg='#F8F8F8', command=Clear3)

ent3.place(x=82, y=184)
but5.place(x=82, y=220)
but6.place(x=274, y=220)

root.mainloop()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GuMondi, 2020-05-28
@GuMondi

You want too much from tkinter, it's just a small graphics library, by the way, when the tkinter window is not in focus, it does not process events, and you need to hang a global hook on the keyboard, then find the tkinter window and take the desired text from it. This is usually written in C++, but since everything boils down to calling winapi functions, you can do everything in python using the pywin32 library

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question