How do I use colour with Windows command prompt using Python? - python

I'm trying to patch a waf issue, where the Windows command prompt output isn't coloured when it's supposed to be. I'm trying to figure out how to actually implement this patch, but I'm having trouble finding sufficient resources - could someone point me in right direction?
Update 1
Please don't suggest anything that requires Cygwin.

It is possible thanks to ctypes and SetConsoleTextAttribute
Here is an example
from ctypes import *
STD_OUTPUT_HANDLE_ID = c_ulong(0xfffffff5)
windll.Kernel32.GetStdHandle.restype = c_ulong
std_output_hdl = windll.Kernel32.GetStdHandle(STD_OUTPUT_HANDLE_ID)
for color in xrange(16):
windll.Kernel32.SetConsoleTextAttribute(std_output_hdl, color)
print "hello"

If you're keen on using normal cmd.exe consoles for the Python interactive interpreter, see this recipe. If you're OK with using special windows simulating a console, for example because you also need more advanced curses functionality anyway, then #TheLobster's suggestion of wcurses is just fine.

Related

How do I clear text on shell?

I need to clear the IDLE shell, using code. The only way I know of to remove the text is closing the shell and reopening. I want this to be able to put into code that requires refreshing the shell, for example a memory game, giving a string of words, and then removing them, or some kind of animation made of text pictures.
Essentially I want to do something like shell.clear() or something similarly easy to use. It can be a function or whatever, but I'd like it to be easy to put into some preexisting code.
I do not know if such a thing is possible, but if you have any pointers, tips or code, I'd appreciate the help.
IDLE 3.7.3 on Mac.
You can use:
from os import system
#for windows
system('cls')
#for Unix based systems
system('clear')
Or if you are inside IDLE or Python interpreter you can use this function:
def cls(): print ("\n" * 100)
That you can call cls() whenever you need to clear your screen.

Python- Full screen nogui?

I made a program in python which allows you to type commands (e.g: if you type clock, it shows you the date and time). But, I want it to be fullscreen. The problem is that my software doesnt have gui and I dont want it to so that probably means that I wont be using tkinter or pygame. Can some of you write a whole 'hello world' program in fullscreen for me? What I am aiming for is for my program to look a bit like ms-dos. Any help??? By the way, im very new to python (approximately 4 weeks).
NOTE: I have python 3.4.1
Since Vista, cmd.exe can no longer go to full-screen mode. You'll need to implement a full-screen console emulator yourself or look for another existing solution. E.g. ConEmu appears to be able to do it.
Solution
Use your Operating System services to configure parameters.
<_aMouseRightClick_>->[Properties]->[Layout]
Kindly notice, that some of the python interpreter process window parameters are given in [char]-s, while some other in [px]:
size.Width [char]-s
size.Height[char]-s
loc.X [px]
loc.Y [px]
So adjust these values so as to please your intentions.
You may set negative values for [loc.X, loc.Y] to move/hide window edges "outside" the screen left/top edges

How return python shell prompt to top of screen?

In python 2.7, how do I not only clear screen but make the prompt begin at the top of the screen. I am surprised that I was not able to find a quick answer for this online. It seems like one of the most basic things someone would desire to be able to do when working with the shell.
If this is a duplicate then by all means direct me to original because I can't find one.
OH yeah, I am on Windows 7.
Maybe you can try the following:
import os
os.system('cls')
If you use the IPython shell (which has many useful features), you can just type:
clear
It is best to just define a function like 'clear()' and use that to repeatedly clear the console.
from os import system
clear = lambda: system('cls') # For Windows
And now you can use 'clear()' whenever you want.

Python 3.x Interaction with other Program GUIs

I'm looking for a Python 3.x library that is able to allow interaction with other programs.
For example, I already have some sort of command-line interface which I have developed in python, and I
want to be able to enter, say "1", and have another program open. From here, I wish to hit another
input like "2" and have it manipulate the GUI that opens (for example, for it to "click" the Configurations
dropdown bar and select an option, perhaps modify a few settings, apply, and then possibly also automatically
enter some text). The reason I'm doing this is for test automation.
I've already tried using pywinauto, but I've found it to not be compatible for Python 3! :(
Is there another possible approach to this? Thanks in advance!!!
P.S. I may have forgotten to mention that I'm using Windows 7 but with Python32
You could look into sikuli. It lets you automate clicks and other actions based on region or matched graphic. Fairly smart. Is there a reason you're dead set on using py3?
Py3-compatible pywinauto released! New home page: http://pywinauto.github.io/
P.S. I'm maintainer of pywinauto.
Late answer, but have a look at pyautogui which enables you to move the mouse and press keys. I used it for the following snippet which launches an emulator and presses keys.
import pyautogui as pg
import os
import time
game_filepath = "../games/BalloonFight.zip"
os.system(f"fceux {game_filepath} &")
time.sleep(1)
keys_to_press = ['s', 's', 'enter']
for key_to_press in keys_to_press:
pg.keyDown(key_to_press)
pg.keyUp(key_to_press)
time.sleep(2)
im = pg.screenshot("./test.png", region=(0,0, 300, 400))
print(im)
A more detailed expalanation can be found here: Reinforcement learning to play Nintendo NES games
I created a pywinauto fork on GitHub that's compatible with Python 3:
https://github.com/Usonaki/sendkeys-py-si-python3
I only did basic testing, so there might still be some circular import related problems that I haven't found.

How to make interactive Python script with keyboard arrows navigation in menu

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.

Categories

Resources