Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Tkinter Text Widget Background Color

Reply
Thread Tools

Re: Tkinter Text Widget Background Color

 
 
Terry Reedy
Guest
Posts: n/a
 
      02-22-2011
On 2/22/2011 6:50 AM, Peter Otten wrote:
> import Tkinter as tk
> from itertools import cycle
>
> root = tk.Tk()
> text = tk.Text(root, font=("Helvetica", 70))
> text.pack()
>
> text.insert(tk.END, "Hello, geocities")
> text.tag_add("initial", "1.0", "1.1")
> text.tag_add("initial", "1.7", "1.8")
>
> colors = cycle("red yellow blue".split())
> initial_colors = cycle("#8f8 #f08".split())
>
> def switch_color():
> # change the complete widget's background color
> text["bg"] = next(colors)
>
> # change the background color of tagged portions
> # of the widget's conten
> text.tag_config("initial", background=next(initial_colors))
>
> # do it again after 300 milliseconds
> root.after(300, switch_color)
>
> # call the color-setting function manually the first time
> switch_color()
> root.mainloop()


This example is helpful to me. I am curious though why the tk window
takes up the full screen instead of being much smaller as usual for
other examples I run. Shortening or shrinking the text has no effect.


--
Terry Jan Reedy

 
Reply With Quote
 
 
 
 
Jeff Hobbs
Guest
Posts: n/a
 
      02-22-2011
On Feb 22, 8:48*am, Terry Reedy <(E-Mail Removed)> wrote:
> On 2/22/2011 6:50 AM, Peter Otten wrote:
>
> > import Tkinter as tk
> > from itertools import cycle

>
> > root = tk.Tk()
> > text = tk.Text(root, font=("Helvetica", 70))
> > text.pack()

>
> > text.insert(tk.END, "Hello, geocities")
> > text.tag_add("initial", "1.0", "1.1")
> > text.tag_add("initial", "1.7", "1.8")

>
> > colors = cycle("red yellow blue".split())
> > initial_colors = cycle("#8f8 #f08".split())

>
> > def switch_color():
> > * * *# change the complete widget's background color
> > * * *text["bg"] = next(colors)

>
> > * * *# change the background color of tagged portions
> > * * *# of the widget's conten
> > * * *text.tag_config("initial", background=next(initial_colors))

>
> > * * *# do it again after 300 milliseconds
> > * * *root.after(300, switch_color)

>
> > # call the color-setting function manually the first time
> > switch_color()
> > root.mainloop()

>
> This example is helpful to me. I am curious though why the tk window
> takes up the full screen instead of being much smaller as usual for
> other examples I run. Shortening or shrinking the text has no effect.


Because a 70point Helvetica font was requested, and the text widget
has a default size of 80x24. That would occupy the full size of most
screens.

Jeff
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      02-22-2011
Terry Reedy wrote:

> On 2/22/2011 6:50 AM, Peter Otten wrote:
>> import Tkinter as tk
>> from itertools import cycle
>>
>> root = tk.Tk()
>> text = tk.Text(root, font=("Helvetica", 70))
>> text.pack()
>>
>> text.insert(tk.END, "Hello, geocities")
>> text.tag_add("initial", "1.0", "1.1")
>> text.tag_add("initial", "1.7", "1.8")
>>
>> colors = cycle("red yellow blue".split())
>> initial_colors = cycle("#8f8 #f08".split())
>>
>> def switch_color():
>> # change the complete widget's background color
>> text["bg"] = next(colors)
>>
>> # change the background color of tagged portions
>> # of the widget's conten
>> text.tag_config("initial", background=next(initial_colors))
>>
>> # do it again after 300 milliseconds
>> root.after(300, switch_color)
>>
>> # call the color-setting function manually the first time
>> switch_color()
>> root.mainloop()

>
> This example is helpful to me. I am curious though why the tk window
> takes up the full screen instead of being much smaller as usual for
> other examples I run. Shortening or shrinking the text has no effect.


The text widget's intended usecase is editing text; therefore it reserves
space for 80 columns and 24 lines by default. Combined with the giant font
size that I specified that means that it takes a all the space it can get.
If you want to (ab)use a text widget to show its content you can specify the
desired size in units of character cells, e. g.

text = tk.Text(root, font=("Helvetica", 70), width=16, height=1)

See also

http://www.tcl.tk/man/tcl8.5/TkCmd/text.htm#M-height

Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Tkinter.event.widget: handler gets name instead of widget. Terry Reedy Python 15 07-16-2012 10:32 AM
Re: Tkinter.event.widget: handler gets name instead of widget. Frederic Rentsch Python 0 07-09-2012 08:39 PM
Tkinter.event.widget: handler gets name instead of widget. Frederic Rentsch Python 0 07-08-2012 09:19 PM
Re: Tkinter.Text widget - how to get text cursor position? Alex9968 Python 0 03-19-2008 07:07 PM
Tkinter.Text widget - how to get text cursor position? Alex9968 Python 0 03-19-2008 05:37 PM



Advertisments