Page 1 of 1

Fancying up a python?

Posted: Mon Jan 11, 2010 6:11 am UTC
by TehT
I've recently started to try my hand at programming in general and decided to try to pick up python, which I've been working with for a couple of months now. I've made several little tools and console based "games" and generally having a blast with it all.

Now the problem comes when i think about going beyond that simple console window, I've briefly looked into pygame and tkinter but trying to integrate either with any code i already have seems kind of pointless at this time. So instead i decided to finish my current project before trying my hand at those.
But does anyone know any alternatives to fancying up the python console under windows? with colors and layout and somesuch.
I looked into curses, but the wincurses module doesn't seem to have been updated since python 2.

Any hints and ideas to where i might go from here is also appreciated :D

Re: Fancying up a python?

Posted: Mon Jan 11, 2010 6:52 am UTC
by Agent_Irons
If you just want to play with graphics and generally get a feel for easybutton graphics, you should try processing. It's a bit like java but designed more as a scripting language for images. It's kind of fun and very easy, and you can compile for the web or for windows,mac, linux, etc.

http://processing.org/learning/

Re: Fancying up a python?

Posted: Mon Jan 11, 2010 11:35 am UTC
by PM 2Ring
Here's an example of doing coloured text in a DOS window. (In Linux, it's easier to do this sort of thing with escape sequences.) I'm pretty sure the original version of this script came from this forum, but unfortunately I've lost the original author's name (& I'm usually careful about that sort of thing).

Code: Select all

#! /usr/bin/env python
# although this script is a bit pointless under *nix :)

""" Experiments with DOS console colours """

import ctypes
import sys

STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE= -11
STD_ERROR_HANDLE = -12

FOREGROUND_BLUE = 0x01 # text color contains blue.
FOREGROUND_GREEN= 0x02 # text color contains green.
FOREGROUND_RED  = 0x04 # text color contains red.
FOREGROUND_INTENSITY = 0x08 # text color is intensified.
BACKGROUND_BLUE = 0x10 # background color contains blue.
BACKGROUND_GREEN= 0x20 # background color contains green.
BACKGROUND_RED  = 0x40 # background color contains red.
BACKGROUND_INTENSITY = 0x80 # background color is intensified.

std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)

class DOScolor(object):
  """ A wrapper for DOS Text color handling """
  SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute
 
  def __init__(self, handle=std_out_handle):
    self.handle = handle
   
  def set_color(self, color):
    """(color) -> BOOL

    Example: set_color(FOREGROUND_GREEN | FOREGROUND_INTENSITY)
    """
    return self.SetConsoleTextAttribute(self.handle, color)
 
def main():
  #Black, Red, Yellow, Green, Cyan, Blue, Magenta, White
  colors = [0, 4, 6, 2, 3, 1, 5, 7]

  dc = DOScolor()

  text = "Here is some rainbow-coloured text"
  for b, a in enumerate(text):
    b = colors[b % 8]
    dc.set_color(b + FOREGROUND_INTENSITY)
    sys.stdout.write(a)

  print
  dc.set_color(7)
  raw_input("Hit Enter to continue")

  #Let's try it another way

  #An infinite looping iterator
  def loop(a):
    while 1:
      for i in a: yield i

  colors = [i | FOREGROUND_INTENSITY for i in colors]

  text = "Here is some more rainbow-coloured text"
  for a, b in zip(text, loop(colors)):
    dc.set_color(b)
    sys.stdout.write(a)   

if __name__ == "__main__":
  main()
  raw_input() #Keep console from closing

Re: Fancying up a python?

Posted: Mon Jan 11, 2010 2:35 pm UTC
by TehT
Ah thanks! thats pretty much what i was looking for! I've come across escapes in my search for a fancier console these last few days, and i think I've actually seen an earlier version of that code, but that snippet is pretty much exactly what i need, thanks :D

Re: Fancying up a python?

Posted: Mon Jan 11, 2010 6:01 pm UTC
by Random832
PM 2Ring wrote:Here's an example of doing coloured text in a DOS window. (In Linux, it's easier to do this sort of thing with escape sequences.)

DEVICE=ANSI.SYS much? (edit: oh, wait, you're using win32. That's not a "DOS window", it's a console window)

Re: Fancying up a python?

Posted: Mon Jan 11, 2010 6:01 pm UTC
by Jorpho
Mightn't IDLE be a good idea, rather than the command-line Python console?

Re: Fancying up a python?

Posted: Mon Jan 11, 2010 7:45 pm UTC
by b.i.o
Agent_Irons wrote:If you just want to play with graphics and generally get a feel for easybutton graphics, you should try processing. It's a bit like java but designed more as a scripting language for images. It's kind of fun and very easy, and you can compile for the web or for windows,mac, linux, etc.

http://processing.org/learning/

Since you already know Python, you may want to look into PyProcessing, a Python package that pretty closely imitates a lot of Processing's functionality. PyGame works well enough for simple stuff though.

Jorpho wrote:Mightn't IDLE be a good idea, rather than the command-line Python console?

Why IDLE? Text editor + CLI works quite well enough, and IDLE has some fun (read: irritating) quirks.

Re: Fancying up a python?

Posted: Tue Jan 12, 2010 4:41 am UTC
by PM 2Ring
Random832 wrote:
PM 2Ring wrote:Here's an example of doing coloured text in a DOS window. (In Linux, it's easier to do this sort of thing with escape sequences.)

DEVICE=ANSI.SYS much? (edit: oh, wait, you're using win32. That's not a "DOS window", it's a console window)

Yeah, sorry. In my defence, I don't know much about Microsoft systems. I never learned about programming in them, and I avoid using them whenever I can. :)

Re: Fancying up a python?

Posted: Tue Jan 12, 2010 2:24 pm UTC
by Jorpho
b.i.o wrote:Why IDLE? Text editor + CLI works quite well enough, and IDLE has some fun (read: irritating) quirks.
Except the CLI isn't in color.

I don't suppose Cygwin is an option? Or is that wincurses again?

Re: Fancying up a python?

Posted: Tue Jan 12, 2010 6:56 pm UTC
by Random832
Jorpho wrote:I don't suppose Cygwin is an option? Or is that wincurses again?


Cygwin has its own ANSI/VT100 emulation

http://cygwin.com/cgi-bin/cvsweb.cgi/sr ... vsroot=src look for "char_command"