Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant use Tkinter entry widget to hold down a key inputed by the user through a function

Tags:

python

tkinter

im new to programing in general and im trying to make a simple program using pyautogui and tkinter to make some basic automation in this case holding a specified key down. However i dont understand what is the problem since the program compiles but nothing really happens heres the code (if u could guide me to some basic learning resources id also be gratefull)

import pyautogui
import time
import keyboard
import random
import win32api, win32con
from tkinter import *

def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,1,1)
    time.sleep(0.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,1,1)

def retrieve():
    hold(key_entry.get())
    print('here')

def hold(key_entry):
    pyautogui.press(key_entry)
    #print(key_entry)
    
    



 
root = Tk()
root.geometry("600x450")

frame = Frame(root)
frame.pack()

key_input_explanation = StringVar()
key_input_explanation.set("input the key u want to be held down. only input a singe key otherwise it wont work")

delay_input_explanation = StringVar()
delay_input_explanation.set("input the delay u want. only input a singe digit otherwise it wont work")
 

#explains the user what to input
label = Label(frame, textvariable = key_input_explanation )
label.pack()

#the user inputs the key to be held down
key_entry = Entry(frame, width = 1)
key_entry.pack(padx = 5, pady = 5)

#explains the user what to input
label = Label(frame, textvariable = delay_input_explanation )
label.pack()

#the user inputs the time before the key is held down
delay_entry = Entry(frame, width = 2)
delay_entry.pack(padx = 5, pady = 5)


#the user must press this to send the info 
button = Button(frame, text = "Submit", command = retrieve)
button.pack(padx = 5, pady = 5)


root.title("why wont this work ")
root.mainloop()







like image 206
Rable Avatar asked Jan 25 '26 16:01

Rable


1 Answers

I worked with your code a little and think I found what you might have been looking for using keyboard.press.

import pyautogui
import time
import keyboard
import random
import win32api, win32con
from tkinter import *


key = ""

repeatPressCount = 10


def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,1,1)
    time.sleep(0.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,1,1)



def hold():
    keyboard.press(key)
    print("Typing " + key)
def retrieve():
    global key
    key = key_entry.get()
    print("The key is: " + key)
    try:
        if key != "" and key != " ":
            for i in range(1,repeatPressCount):
                root.after(int(delay_entry.get())*1000, hold())
    except:
        pass
root = Tk()
root.geometry("600x450")

frame = Frame(root)
frame.pack()

#explains the user what to input
label = Label(frame, text = "input the key u want to be held down. only input a singe key otherwise it wont work" )
label.pack()

#the user inputs the key to be held down
key_entry = Entry(frame, width = 1)
key_entry.pack(padx = 5, pady = 5)

#explains the user what to input
label2 = Label(frame, text = "input the delay u want. only input a singe digit otherwise it wont work" )
label2.pack()

#the user inputs the time before the key is held down
delay_entry = Entry(frame, width = 2)
delay_entry.pack(padx = 5, pady = 5)

#the user must press this to send the info 
button = Button(frame, text = "Submit", command = retrieve)
button.pack(padx = 5, pady = 5)

root.title("why wont this work ")

def exit(event):
    root.destroy()
# press escape to exit
root.bind("<Escape>", exit)

root.mainloop()
like image 110
Jamin Avatar answered Jan 27 '26 08:01

Jamin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!