Pmw ScrolledText Widget

  • Thread starter Andrew Wheatley
  • Start date
A

Andrew Wheatley

In the text component, I use the insert method to insert a line of
text.
Initially, I set the foreground colour of the text component to
yellow, that is,
all text is yellow. What I'd like to do is have different strings
have different coloured text. You can't change the foreground color
of the text component without changing all the strings to the same
colour. Anyone know how to have
different strings have different colours?

Here's a code snippet:

# One time creation of the widget
def AddMessageBox (self,parent):

self._messageBox = Pmw.ScrolledText(parent,
hull_width = 850,
hull_height = 100,
usehullsize=1)
self._messageBox.grid(row=0,column=0)

# Background is black, text color is yellow
self._messageBox.component('text').configure(background='black')
self._messageBox.component('text').configure(foreground='yellow')

# Create a filename
self._logfile = "Vcalc_Session_Log_"
timestring = time.strftime("%d_%b_%Y_%H_%M_%S",time.localtime())
self._logfile = "%s%s" % (self._logfile,timestring)

# First line in the message box is yellow
self._messageBox.insert('end','1: Log file is %s\n' %
(self._logfile,))
self._messageBoxLineCount = 2

# Whenever this method is called in appends a string into the text
widget
# of the ScrolledText widget. The 3rd parameter 'color' is supposed
to
# set the color of the string without changing all the previous
strings
# but I don't know how to do it yet.
def LogMessage(self,string,color):

output = '%d: %s' % (self._messageBoxLineCount,string,)
self._messageBox.insert('end',output)

self._messageBoxLineCount = self._messageBoxLineCount + 1
 
M

Michael Peuser

Andrew Wheatley said:
In the text component, I use the insert method to insert a line of
text.
Initially, I set the foreground colour of the text component to
yellow, that is,
all text is yellow. What I'd like to do is have different strings
have different coloured text. You can't change the foreground color
of the text component without changing all the strings to the same
colour. Anyone know how to have
different strings have different colours?

Text is a very versatile widget! Read the spec! You will have to use tags.

from Tkinter import *

t=Text()
t.pack()
t.tag_config('nice',background='green')
t.tag_config('reverse',background='black',foreground='white')

t.insert('end',"NICE","nice","REVERSE","reverse")
mainloop()


Kindly
Michael P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top