Re: Tkinter Text Widget Background Color

Discussion in 'Python' started by Terry Reedy, Feb 22, 2011.

  1. Terry Reedy

    Terry Reedy Guest

    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
     
    Terry Reedy, Feb 22, 2011
    #1
    1. Advertising

  2. Terry Reedy

    Jeff Hobbs Guest

    On Feb 22, 8:48 am, 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.


    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
     
    Jeff Hobbs, Feb 22, 2011
    #2
    1. Advertising

  3. Terry Reedy

    Peter Otten Guest

    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
     
    Peter Otten, Feb 22, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pavel.kosina

    Tkinter - pixel or widget color

    pavel.kosina, Apr 4, 2005, in forum: Python
    Replies:
    1
    Views:
    549
    Jeff Epler
    Apr 4, 2005
  2. Joe Van Dyk
    Replies:
    2
    Views:
    194
    Martin DeMello
    Jul 21, 2005
  3. Frederic Rentsch
    Replies:
    0
    Views:
    246
    Frederic Rentsch
    Jul 8, 2012
  4. Terry Reedy
    Replies:
    15
    Views:
    637
    Frederic Rentsch
    Jul 16, 2012
  5. Frederic Rentsch
    Replies:
    0
    Views:
    430
    Frederic Rentsch
    Jul 9, 2012
Loading...

Share This Page