[Tutor] GUI for ANSI colors

Alan Gauld alan.gauld at yahoo.co.uk
Mon Feb 13 06:28:43 EST 2017


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




More information about the Tutor mailing list