Fancying up a python?

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

TehT
Posts: 13
Joined: Tue Jun 03, 2008 2:41 pm UTC

Fancying up a python?

Postby TehT » Mon Jan 11, 2010 6:11 am UTC

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

Agent_Irons
Posts: 213
Joined: Wed Sep 10, 2008 3:54 am UTC

Re: Fancying up a python?

Postby Agent_Irons » Mon Jan 11, 2010 6:52 am UTC

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/

User avatar
PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Fancying up a python?

Postby PM 2Ring » Mon Jan 11, 2010 11:35 am UTC

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

TehT
Posts: 13
Joined: Tue Jun 03, 2008 2:41 pm UTC

Re: Fancying up a python?

Postby TehT » Mon Jan 11, 2010 2:35 pm UTC

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

Random832
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: Fancying up a python?

Postby Random832 » Mon Jan 11, 2010 6:01 pm UTC

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)
Last edited by Random832 on Mon Jan 11, 2010 6:02 pm UTC, edited 1 time in total.

User avatar
Jorpho
Posts: 6279
Joined: Wed Dec 12, 2007 5:31 am UTC
Location: Canada

Re: Fancying up a python?

Postby Jorpho » Mon Jan 11, 2010 6:01 pm UTC

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

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Fancying up a python?

Postby b.i.o » Mon Jan 11, 2010 7:45 pm UTC

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.

User avatar
PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Fancying up a python?

Postby PM 2Ring » Tue Jan 12, 2010 4:41 am UTC

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. :)

User avatar
Jorpho
Posts: 6279
Joined: Wed Dec 12, 2007 5:31 am UTC
Location: Canada

Re: Fancying up a python?

Postby Jorpho » Tue Jan 12, 2010 2:24 pm UTC

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?

Random832
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: Fancying up a python?

Postby Random832 » Tue Jan 12, 2010 6:56 pm UTC

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"


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 5 guests