Tk read-only text box

Discussion in 'Ruby' started by nornagon, May 23, 2005.

  1. nornagon

    nornagon Guest

    Another Tk question.

    Perl/Tk has a widget called Tk::ROText, which acts just like a text
    box, with the exception that the user cannot enter text into it.

    I need something with the same functionality; I would like the user to
    be able to select the text and scroll through it, but not edit it.
    Also, I need to be able to change its contents dynamically.

    Thanks,
    --=20
    - nornagon
    nornagon, May 23, 2005
    #1
    1. Advertising

  2. From: nornagon <>
    Subject: Tk read-only text box
    Date: Tue, 24 May 2005 06:56:06 +0900
    Message-ID: <>
    > I need something with the same functionality; I would like the user to
    > be able to select the text and scroll through it, but not edit it.
    > Also, I need to be able to change its contents dynamically.


    Hmmm.... Probably there are some ways.
    One of the ways is "block Key events".
    To do that, please use Tk.callback_break.
    For example,
    ---------------------------------------------------
    require 'tk'
    t = TkText.new.pack
    t.bindtags_unshift(b = TkBindTag.new)
    b.bind('Key', proc{ Tk.callback_break })

    t.insert:)end, "This is a test message.\n");

    Tk.after(10000){t.insert:)end, "This is another test message.\n")}

    Tk.mainloop
    ---------------------------------------------------
    --
    Hidetoshi NAGAI ()
    Hidetoshi NAGAI, May 24, 2005
    #2
    1. Advertising

  3. nornagon

    nornagon Guest

    On 5/24/05, Hidetoshi NAGAI <> wrote:
    > From: nornagon <>
    > Subject: Tk read-only text box
    > Date: Tue, 24 May 2005 06:56:06 +0900
    > Message-ID: <>
    > > I need something with the same functionality; I would like the user to
    > > be able to select the text and scroll through it, but not edit it.
    > > Also, I need to be able to change its contents dynamically.

    >=20
    > Hmmm.... Probably there are some ways.
    > One of the ways is "block Key events".
    > To do that, please use Tk.callback_break.
    > For example,
    > ---------------------------------------------------
    > require 'tk'
    > t =3D TkText.new.pack
    > t.bindtags_unshift(b =3D TkBindTag.new)
    > b.bind('Key', proc{ Tk.callback_break })
    >=20
    > t.insert:)end, "This is a test message.\n");
    >=20
    > Tk.after(10000){t.insert:)end, "This is another test message.\n")}
    >=20
    > Tk.mainloop
    > ---------------------------------------------------


    Interesting. Could you tell me how that works, exactly? Like, what's
    bindtags_unshift do? What's a BindTag, even?

    Thanks a lot for your patience. ^_^

    --=20
    - nornagon
    nornagon, May 24, 2005
    #3
  4. From: nornagon <>
    Subject: Re: Tk read-only text box
    Date: Tue, 24 May 2005 15:40:25 +0900
    Message-ID: <>
    > > For example,
    > > ---------------------------------------------------
    > > require 'tk'
    > > t = TkText.new.pack
    > > t.bindtags_unshift(b = TkBindTag.new)
    > > b.bind('Key', proc{ Tk.callback_break })
    > >
    > > t.insert:)end, "This is a test message.\n");
    > >
    > > Tk.after(10000){t.insert:)end, "This is another test message.\n")}
    > >
    > > Tk.mainloop
    > > ---------------------------------------------------

    >
    > Interesting. Could you tell me how that works, exactly? Like, what's
    > bindtags_unshift do? What's a BindTag, even?


    Each widget has a bindtag list to determine bindings which should be
    appleid. The default value of the list is [<self>, <class of the
    widget>, <toplevel which the widget is placed on>, 'all'].
    When an event occurs on the widget, one or none of the procedures
    binded to each bindtag is called. Of course, the event sequence of
    the binding has to matche the event. Usually, the calling process is
    done in order of the bindtag list. Tk.callback_break can break the
    sequence and complete the operation for the event.

    You can get/set the bindtag list by bindtags/bindtags= method. And can
    add a new bindtag to the head of the list by bindtags_unshift method
    (remove the head by bindtags_shift method).

    If you remove <class of the widget> from a bindtag list of a widget,
    the widget lose all bindings of the widget class. If add <class of the
    widget> again, the widget gets the bindings of the widget class again.

    Well, in the example case, a new bindtag is inserted at head of the
    bindtag list of the text widget. The bindtag has a binding for all
    "Key" events. And the binding is "Tk.callback_break", that is, "finish
    the operation for the event".

    The operataion "insert a key-pressed character" is one of the bindings
    for a kind of "Key" event. And it is binded on <class of the widget>,
    that is, "TkText".

    However, on the example, all kind of "Key" events are blocked on the
    new bindtag and aren't checked on "TkText" tag. So, keyboard events
    cannot change the text widget.

    # The reason of why don't remove "TkText" tag is to be able to accept
    # mouse events.
    --
    Hidetoshi NAGAI ()
    Hidetoshi NAGAI, May 24, 2005
    #4
  5. nornagon

    nornagon Guest

    On 5/24/05, Hidetoshi NAGAI <> wrote:
    > From: nornagon <>
    > Subject: Tk read-only text box
    > Date: Tue, 24 May 2005 06:56:06 +0900
    > Message-ID: <>
    > > I need something with the same functionality; I would like the user to
    > > be able to select the text and scroll through it, but not edit it.
    > > Also, I need to be able to change its contents dynamically.

    >=20
    > Hmmm.... Probably there are some ways.
    > One of the ways is "block Key events".
    > To do that, please use Tk.callback_break.
    > For example,
    > ---------------------------------------------------
    > require 'tk'
    > t =3D TkText.new.pack
    > t.bindtags_unshift(b =3D TkBindTag.new)
    > b.bind('Key', proc{ Tk.callback_break })
    >=20
    > t.insert:)end, "This is a test message.\n");
    >=20
    > Tk.after(10000){t.insert:)end, "This is another test message.\n")}
    >=20
    > Tk.mainloop
    > ---------------------------------------------------


    Hmm. I've been using this for a while and it works quite nicely.
    However, I've noticed that it prevents you from copying from the text
    box using Ctrl+C. Is there a way to fix this?

    --=20
    - nornagon
    nornagon, May 25, 2005
    #5
  6. From: nornagon <>
    Subject: Re: Tk read-only text box
    Date: Wed, 25 May 2005 21:37:04 +0900
    Message-ID: <>
    > Hmm. I've been using this for a while and it works quite nicely.
    > However, I've noticed that it prevents you from copying from the text
    > box using Ctrl+C. Is there a way to fix this?


    Here is an example.
    ----------------------------------------------------------------
    require 'tk'
    t = TkText.new.pack
    t.bindtags_unshift(b = TkBindTag.new)
    b.bind('Key', proc{ Tk.callback_break })
    b.bind('Control-KeyPress-w',
    proc{ Tk.callback_continue }) # continue to the next bindtag
    b.bind('Control-KeyPress-y', proc{})
    b.bind('Control-KeyPress-c', proc{t.insert:)end, "Press Ctrl-C !!\n")})
    b.bind('Control-Alt-KeyPress-c', proc{exit})

    t.insert:)end, "This is a test message.\n");

    Tk.after(10000){t.insert:)end, "This is another test message.\n")}

    Tk.mainloop
    ----------------------------------------------------------------
    For one event, only one binding is called on each bindtag.
    That is most detailed one which matched to the event.
    --
    Hidetoshi NAGAI ()
    Hidetoshi NAGAI, May 25, 2005
    #6
  7. From: Hidetoshi NAGAI <>
    Subject: Re: Tk read-only text box
    Date: Wed, 25 May 2005 23:21:52 +0900
    Message-ID: <>
    > Here is an example.
    > ----------------------------------------------------------------
    > require 'tk'
    > t = TkText.new.pack
    > t.bindtags_unshift(b = TkBindTag.new)
    > b.bind('Key', proc{ Tk.callback_break })
    > b.bind('Control-KeyPress-w',
    > proc{ Tk.callback_continue }) # continue to the next bindtag
    > b.bind('Control-KeyPress-y', proc{})
    > b.bind('Control-KeyPress-c', proc{t.insert:)end, "Press Ctrl-C !!\n")})
    > b.bind('Control-Alt-KeyPress-c', proc{exit})
    >
    > t.insert:)end, "This is a test message.\n");
    >
    > Tk.after(10000){t.insert:)end, "This is another test message.\n")}
    >
    > Tk.mainloop
    > ----------------------------------------------------------------


    It goes without saying that the following is same to replace
    the standard callback function of the text widget.
    ----------------------------------------------------
    b.bind('Control-KeyPress-y', proc{puts "Press Ctrl-y!!\n"; Tk.callback_break})
    ----------------------------------------------------
    --
    Hidetoshi NAGAI ()
    Hidetoshi NAGAI, May 25, 2005
    #7
    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. mit
    Replies:
    1
    Views:
    786
    Ramu Pulipati
    Jan 25, 2006
  2. Julie

    Text boxes and read-only text

    Julie, May 12, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,066
    Julie
    May 12, 2004
  3. Gnarlodious
    Replies:
    4
    Views:
    17,555
    elizas
    May 5, 2010
  4. keithb
    Replies:
    2
    Views:
    7,990
    keithb
    Jun 7, 2006
  5. Randell D.
    Replies:
    10
    Views:
    163
Loading...

Share This Page