Re: [Tutor] Fwd: Re: GUI for ANSI colors

2017-02-13 Thread Alan Gauld via Tutor
On 14/02/17 00:58, Alan Gauld forwarded:

> red = '\033[91m'
> yel = '\033[93m'
> blu = '\033[34m'
> grn = '\033[32m'

These are indeed the ANSI codes for the colours but ANSI
codes only work in an ANSI terminal and GUIs are not
ANSI terminals. Instead they have their own ideas on
colours and you need to use those. They all support
the standard 16 colours used in ANSI however, so in
your case (for Tkinter at least) just use the names
'red', 'yellow', 'blue' and 'green'

> colors = [red, yel, blu, grn]

colors = ['red', 'yellow', 'blue', 'green']


> final_word = ''.join(choice(colors) + char for char in word)

But again that won't work in a GUI, you need to use the GUIs
way of colouring. In Tkinter that's via tags as per my
previous post, reproduced here).

> ###
> from Tkinter import *
> 
> n = 0
> top = Tk()
> t = Text(top, width=25, height=2)
> t.pack()
> 
> # set up tags for the colors
> colors = ['red','blue','green']
> for col in colors:
> t.tag_config(col, foreground=col)
> 
> def addWords():
> n = 0
> for word in "Hello ", "bright ", "world ":
> t.insert(END,word,colors[n])  #use the color tags
> n +=1
>

You need to change the loop to iterate over the letters
instead of words and choose the tags at random. But that
should be fairly straightforward.

> Button(top,text="Add words",command=addWords).pack()
> 
> top.mainloop()
> ##


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Re: GUI for ANSI colors

2017-02-13 Thread Alan Gauld via Tutor
Forwarding to list.

---
Ive tried your suggestion but didnt work. Look this is my code and i
need to print final_word into GUI. Im working on Ubuntu 16.04

#! usr/bin/python3

from random import choice
from string import ascii_letters, digits

chars = ascii_letters + digits
word = ''.join(choice(chars) for i in range(12))

red = '\033[91m'
yel = '\033[93m'
blu = '\033[34m'
grn = '\033[32m'

colors = [red, yel, blu, grn]

final_word = ''.join(choice(colors) + char for char in word)
print(final_word)

2017-02-13 12:28 GMT+01:00 Alan Gauld via Tutor >:

On 13/02/17 09:40, Freedom Peacemaker wrote:

> I'm trying to put my script with randomly colored letters (ANSI escape
> code) into GUI but cant find any. I've tried tkinter but it isn't good
> choice.

Why not? It can certainly do what you want. As can virtually
any other GUI toolkit. But programming GUIs is much harder
than programming the console regardless of toolkit choice.

Here is a sample Tkinter program that displays text in
different colors:

###
from Tkinter import *

n = 0
top = Tk()
t = Text(top, width=25, height=2)
t.pack()

# set up tags for the colors
colors = ['red','blue','green']
for col in colors:
t.tag_config(col, foreground=col)

def addWords():
n = 0
for word in "Hello ", "bright ", "world ":
t.insert(END,word,colors[n])  #use the color tags
n +=1

Button(top,text="Add words",command=addWords).pack()

top.mainloop()
##

Most other GUIs will be similar.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld

Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



___
Tutor maillist  -  Tutor@python.org 
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor