Python Curses: Controlling Two Terminals with One Script (Linux) - python

I would like to make a single Python script to control curses in two seperate terminal windows. Not curses windows within a single terminal, but two different instances of, say, urxvt in my X window manager.
Something like this:
class myprogam():
controlterm1()
controlterm2()
def controlterm1():
Create a new (could be current) urxvt terminal window.
In the urxvt window setup curses with curses.initscr(), etc.
racergame()
def controlterm2():
Create another urxvt terminal window.
Setup curses.initscr, borders, size, etc. in window, leaving the old one alone.
typewriter()
def racecargame():
Racing game in curses
def typwritter()
Boring program for writing
myprogram()
I see that pseudo-terminal windows are given a name when I use the command "tty", and this gives me a reference to /dev/pts/somenumber, where the first terminal emulator is assigned a zero, then 1, 2, etc for new terminals. Can I use this identifier as a basis to do this?
The script does NOT have to actually load a new instance of urxvt, as I am happy if it can just control one that is already open.

In principle there is no problem with opening multiple devices and doing I/O to them from one script. HOWEVER, most curses software that I've seen assumes it does I/O to the one terminal associated with the program. For example, initscr takes no parameters. It looks at your environment variables to determine your terminal characteristics.
Given that, it might be easier to split your program into two, one running on each terminal, which communicate via message-passing.

The ncurses C API has the newterm function that can be used to initialize the screen. According to the newterm man page:
A program that outputs to more than one terminal should use the newterm routine for each terminal instead of initscr.
However, the python curses module does not implement newterm.

Related

Determing Window Size from within Python is not answered by reference to Linux

Is there a way within Python, perhaps using sys and os, to determine the size of the window in which you are running Python. I can do this in emacs using keyboard to run an emacs function, but not in general.
I did not work too hard at it, but the other answers (for me) failed in Windows using Anaconda3 Python. Perhaps someone else could try them in an emacs environment, which is where I tried them. They do return answers that appear to be defaults because they do not match my window size.
You can use os.get_terminal_size:
import os
os.get_terminal_size()
#os.terminal_size(columns=80, lines=24)
and this class returned supports unpacking:
columns, lines = os.get_terminal_size()
If you are running Python in a graphics window, at least on Windows, there is, in general, no way to get the window size from within the Python program. If, for example, one starts IDLE from a Windows console, shutil.get_window_size() reports the size of the parent console (rows and columns), not the size of the tk text widget that IDLE's shell is running in. If one starts IDLE otherwise, from an icon or File Explorer, one gets the default 24 x 80.

Bring terminal to the front in Python

Is there a way to bring the Linux terminal to the front of your screen from a python script? Possibly using some kind of os. command
I.e - Your python script opens up a GUI that fills the screen, but if a certain event happens that you want to see printed in the terminal to be viewed, but don't want to / can't show this information on the GUI (so please don't suggest that)
And if possible, hide it back behind your other windows again, if needed.
(Python 2, by the way)
Any suggestions greatly appreciated.
Not in any generally supported way.
Some terminal applications may support the following control sequences. However, these sequences are not standardized, and most terminals do not implement them.
\e[5t - move window to front
\e[6t - move window to back
\e[2t - minimize ("iconify") window
\e[1t - un-minimize window
— from http://rtfm.etla.org/xterm/ctlseq.html
That "bring the Linux terminal to the front of your screen" is likely talking about terminal emulators running in an X Window environment. Ultimately this is accomplished by making a request to the window manager. There is more than one way to do this.
xterm (and some other terminal emulators) implement the Sun control sequences for window manipulation (from the 1980s) which were reimplemented in dtterm during the early 1990s. xterm has done this since 1996 (patch #18).
Python: Xlib — How can I raise(bring to top) windows? mentions wmctl, a command-line tool which allows you to make various requests to the window manager.
xdotool is another command-line tool which performs similar requests.
finally, Can a WM raise or lower windows? points out that you can write your own application (and because python can use shared libraries written in C, you could write a script using the X library).

Can I keep the terminal as the active window, even if it's in the background? - Python 3.3

G'day,
I've just posted this question here. Following on from that, is there a means to lock keyboard user input to the terminal, when it's running behind another window? My system requires a user to scan their barcode (barcode scanner acts as a keyboard. ie. outputs a string of letters and presses enter) inside the terminal. However, the system also requires that a log CSV file be displayed on the attached monitor. As such, with the terminal in the background, the cursor automatically reverts to the log CSV file when opened, which disables the users' barcode scan from being entered into the terminal.
I'm still relatively new to Python, and haven't completely figured out the functionality of this system. I will eventually set it up such that when the system boots, the log file will automatically open on top, with the terminal (and cursor input) running in the background.
Again, I don't have any code to demonstrate my attempts, but I have done extensive research. The only thing I've found that may offer this functionality is xdotool. I could automatically rearrange the windows such that the terminal was always at the back, and somehow automatically allocate the terminal as the 'active' window?
Any help here would be great!
Thanks!

Howto to show program window in specific desktop using python

I am writing python app and need to run another program in specific (not the current one) desktop in openbox (I have 4 desktops). There is freedesktop _NET_CURRENT_DESKTOP param, but I don't know how to change it under python and gi.repository (Wnck, Gdk?). Or maybe use Xlib? I can't find what is best method to do it...
With PyGTK, you would create a gio.AppInfo instance representing the application you want to launch, then provide a gtk.gdk.AppLaunchContext to its launch() method.
When you create the launch context, you would call its set_desktop() method to specify the index of the desktop the launched application will run on.

Python: Opening process on a certain screen

I wrote a script for myself so when I login to the computer 4 IE windows auto start. I tried to use the webbrowser module but it would load the 4 links into 1 IE window with tabs. I could not get webbrowser to open 4 different windows so I used subprocess.Popen.
I have 4 monitors and I want each instance of IE to automatically start on the appropriate screen. Right now I can only get the 4 windows to load behind eachother on the main screen. How can I accomplish this? I have googled and googled and I cannot find anything about opening the process with certain dimensions or on a certain screen... Here is my script:
import subprocess
subprocess.Popen('"C:\\Program Files\\Internet Explorer\\iexplore.exe" hxxps://myserver.com/Orion/SummaryView.aspx?viewid=1')
subprocess.Popen('"C:\\Program Files\\Internet Explorer\\iexplore.exe" hxxp://myserver2.com:8080/WOListView.do')
subprocess.Popen('"C:\\Program Files\\Internet Explorer\\iexplore.exe" hxxp://myserver3.com:8888/stats/cgi?sid=301960859109&area=stats&action=noc&id=22689236889&page=22&sel=tab_listview_sel_22689236889')
subprocess.Popen('"C:\\Program Files\\Internet Explorer\\iexplore.exe" hxxps://mymail.com/owa/')
I doubt you can do this with Python standard cross-platform functions alone. Using Windows API you can specify application window starting position and size with dwX, dwY, dwXSize, dwYSize members of the STARTUPINFO structure passed to the CreateProcess function. There are probably examples out there of using CreateProcess with Python ctypes FFI facility. Multi-monitor setup essentially provides one big virtual desktop with continuous coordinate system, so by setting these parameters you can make each window to appear on a separate monitor. Again, you need Windows API to determine coordinates of each monitor inside this big virtual screen space, namely EnumDisplayMonitors and GetMonitorInfo functions. Or, since you are probably never going to use it on any other machine, you can determine screen offsets experimentally and hardcode them.

Categories

Resources