Skip to content
← Back to Community
How to Simulate Key Presses in Python
Profile icon
Futuristics

I have been searching the web but have been unable to find ANYTHING that is for Linux and that can type sentences, not just letters. I have a Tkinter textbox and I have been trying to find out how to make the program be able to type in the Tkinter textbox. For solutions, I have found things like the SendKeys module but these don't work as they are only for Windows. So if anyone knows how to make python code be able to type in a Tkinter textbox, or know a module that simulates keypresses in python, PLEASE let me know. Thanks!

Voters
Profile icon
Futuristics
Comments
hotnewtop
Profile icon
Coder100

is this good?

If that doesn't work, then try pyautogui

If neither work, that means repl.it is bad, and you should report to bugs

Profile icon
Futuristics

@Coder100 Thanks for the speedy answer. But no, I have tried that already and for some reason it brnigs up some errors in Tkinter for Repl

Profile icon
Futuristics

@Coder100 Ok let me test it

Profile icon
Coder100
Profile icon
Futuristics

@Coder100 Thanks! But for some reason its taking forever to load

Profile icon
Coder100

send repl @Futuristics

Profile icon
Futuristics

@Coder100 Wait its starting let me check it

Profile icon
Coder100

aweesome gl @Futuristics

Profile icon
Futuristics

@Coder100 Pyautogui doesn't work

Profile icon
Futuristics

@Coder100 First of all it took 20 minutes for everything to load, second of all when run it gave an error. I'm gonna try it one las ttime to see if it works

Profile icon
Coder100

send the error @Futuristics

Profile icon
Futuristics

@Coder100 Traceback (most recent call last):
File "main.py", line 5, in
import pyautogui
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pyautogui/init.py", line 242, in
import mouseinfo
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/mouseinfo/init.py", line 223, in
_display = Display(os.environ['DISPLAY'])
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/Xlib/display.py", line 80, in init
self.display = _BaseDisplay(display)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/Xlib/display.py", line 62, in init
display.Display.init(*(self, ) + args, **keys)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/Xlib/protocol/display.py", line 53, in init
name, host, displayno, screenno = connect.get_display(display)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/Xlib/support/connect.py", line 62, in get_display
return mod.get_display(display)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/Xlib/support/unix_connect.py", line 47, in get_display
raise error.DisplayNameError(display)
Xlib.error.DisplayNameError: Bad display name "MAGIC"

Profile icon
Futuristics

@Coder100 I sent you the error

Profile icon
Coder100

oh, use pygame instead @Futuristics

Profile icon
Futuristics

@Coder100 Would Pygame be compatible with tkinter?

Profile icon
Coder100

oh wait you are already on a repl that has a pre-built display? @Futuristics

Profile icon
Futuristics

@Coder100 What do you mean?

Profile icon
Coder100

so pygame and tkinter repls have a built-in display @Futuristics

Profile icon
Futuristics

@Coder100 I made a python project, and am using tkinter in it

Profile icon
Coder100

oh, make a tkinter repl @Futuristics

Profile icon
Futuristics

@Coder100 And what would that fix?

Profile icon
Futuristics

@Coder100 When I put it in Tkinter, I got an error when trying to import some modules.

Error:
Repl.it: Installing fresh packages

ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
WARNING: You are using pip version 20.1.1; however, version 20.2.3 is available.
You should consider upgrading via the '/opt/virtualenvs/python3/bin/python3 -m pip install --upgrade pip' command.

Repl.it: package installation failed!

Profile icon
Futuristics

@Coder100 Please answer quickly. Thanks!