I need text-console interactive dialogs. I am porting a shell script to Python. Which Python tool would do pretty much exactly what the UNIX command dialog does? (i.e. ready-to-use yes/no, calendar, text, gauge, etc. controls which return results and are ncurses-like full-screen UIs)
You should look at Urwid.
Urwid provides a higher-level toolkit on top of curses and includes an event loop to handle keyboard and mouse input. It either uses it's own select-based event loop, or can hook into gevent or Twisted.
In addition to handling keyboard input efficiently you'll also have a host of options to handle user input with edit boxes, list controls and more.
Have a look at curses. It's low-level, but has the advantage of being a part of the standard library.
These are dialog Python bindings: http://pythondialog.sourceforge.net/
Related
Basically I would like to write small script that would allow me to have some sort of programmable keyboard emulation. Something similar to how autohotkey on Windows is able to work.
Lets say I would like to rebind arrow keys to 'wsad' or 'hjkl' but only when CapsLook is active. I was able to detect keyboard key press with pyinput(https://pypi.python.org/pypi/pynput ) I also can send easily various keyboard events to focused window with pyautogui (https://pyautogui.readthedocs.io) But I can't figure out a way to consume events before they are received by currently focused window.
Any hints?
THIS module is one of the available tools for capturing keyboard events:
https://pypi.python.org/pypi/keyboard/
but it is still in the development and doesn't (yet) provide a global hook capable of capturing keyboard events at their very origin and forwarding them (or not) to the target application.
Another tool worth to look into is:
myboard.py at code.google.com downloads
The above script is using Python ctypes and Xlib modules which makes it possible to work directly with the system libraries written in C. It catches the keyboard events quite deep and system wide to a degree that it had crashed my OS while testing it a bit, so be warned ...
Consider also using XGrabKey and XGrabKeyboard from the X11 libX11.so system library:
import ctypes
libX11 = ctypes.CDLL('libX11.so')
XGrabKey = libX11.XGrabKey
XGrabKeyboard = libX11.XGrabKeyboard
print("XGrabKey: " , dir(XGrabKey))
print("XGrabKeyboard: ", dir(XGrabKeyboard))
I need to write a virtual keyboard for typing texts, I'm planning to use Python and Qt (PyQt) library for this task. problem is I don't know how to simulate KeyPress not as internal Qt event, but as simulation of a real keyboard so I could work with this keyboard as like with real one - interacting with application on my computer. I can't find anything in Qt documentation about this. So is there any way to do it through PyQt/Qt, or I need to use some Python library, and which exactly?
I understand that this is a PyQt question, but at the request of the OP will give a c++ example in case it helps in finding the Python solution.
On the c++ side simulating the keyboard is done by posting keypress events to the application's event loop. These may be considered 'internal Qt events', but are the exact same interface as would be received for a physical key press. They are accomplished as follows:
QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter);
QCoreApplication::postEvent (receiver, event);
Looking through the PyQt QCoreApplcation API, the postEvent function also exists, so it should be possible to do something analagous (unfortunately I can't offer an example as I'm unfamiliar with writting python scripts).
I had the same problem.
Pyautogui is very good for this and it is stupid simple.
import pyautogui
pyautogui.typewrite("the stuff")
Or if you want to actually simulate pressing a literal keypress use:
import pyautogui
pyautogui.keypressDown("the stuff")
pyautogui.keypressUp("the stuff")
Here's the documentation: https://pyautogui.readthedocs.org/en/latest/
Hope this helps.
I'm not even sure what these would be called? I used to write them in ADA running on VAX-VMS!
I want to make a simple menu-driven application that would let me display menus and use the cursor keys to navigate around them, choose items and navigate around the app. All fully old school text based.
I want to be able to take over the whole terminal window and display stuff in different places.
Is this possible in Python?
Check out the Python HOWTO on Curses Programming with Python and here is the library reference.
Another easy to use library is Urwid - Console User Interface Library.
http://excess.org/urwid/
http://excess.org/urwid/examples.html
Yes, have a look at the different curses implementations.
I would like to create interactive Python script (probably using curses?) with menu where user can navigate over menu using keyboard arrows.
What is the easiest way to implement such functionality? Any simple usage/tutorial?
Thank You!
take a look at Python curses module and for example ncurses-ui-python.
There are alternatives like Urwid and Pycdk
You might wanto to take a look also at snack (newt-based library)
You might also want to check out Unicurses, which wraps python's native curses module in linux and pdcurses in Windows.
Unicurses has the benefit of maintaining a style of usage which is consistent with nCurses and PDCurses. Therefore, tutorials such as this one will be more helpful if you're wrapping puthon curses with unicurses.
When you tkSimpleDialog.askinteger, the program stalls and waits for user input. What are the basics of writing my own method that would have the same effect? I want to make the same kind of dialog box, I just want to be able to request more information.
The problem that I'm having is that when I open the new window using Tk.Toplevel, the program does not wait for user input the way tkSimpleDialog.askinteger does.
First off, if you can use some other widget system like PyGtk or PyQt, you should seriously consider it. Tkinter is ancient, and the newer libraries have a lot more functionality (read: more things you don't have to reinvent). I've used PyGtk and like it a lot more than Tkinter, which I used in the old Python 1.x days.
That said, in Tkinter, you need to do this:
widget.wait_window(window)
This ties up the event loop waiting for the user to dismiss the dialog.
Reference: http://www.pythonware.com/library/tkinter/introduction/dialog-windows.htm