Tkinter: cut'n'paste from DISABLED window?

Peter Abel PeterAbel at gmx.net
Wed Apr 7 04:06:04 EDT 2004


"Patrick L. Nolan" <pln at cosmic.stanford.edu> wrote in message news:<c4vm3g$b2d$1 at news.Stanford.EDU>...
> Our Tkinter application has a big ScrolledText widget which is
> a log of everything that happens.  In order to prevent people
> from making changes, we have it in DISABLED mode except when
> the program wants to write a new entry.  This works OK, except
> that sometimes we want to copy out of piece of the contents and
> paste it in another window.  When it's DISABLED, it appears
> that we can't even select a portion of the text.
> 
> Is this an either-or situation?  Or is there some clever way to
> get around it?
> 
> If it matters, we want this to work on both Linux and Windows.

The question is if you really need a Tkinter.Text-widget or if a
Tkinter.Listbox would satisfy your wishes. Then you could program
a scrollable Listbox as the following example:

    # Vertical scrollbar for listbox
    vscroll=Tkinter.Scrollbar(self,orient='vertical')
    vscroll.grid(row=3,rowspan=10,column=11,sticky='ns')

    # horizontal scrollbar for listbox
    hscroll=Tkinter.Scrollbar(self,orient='horizontal')
    hscroll.grid(row=13,column=2,columnspan=9,sticky='ew')

    # Listbox
    self.msgBox=Tkinter.Listbox(self,
                                bg='white',
                                relief='sunken',
                                xscrollcommand=hscroll.set,
                                yscrollcommand=vscroll.set,
                                )
    self.msgBox.grid(row=3,rowspan=10,column=2,columnspan=9,sticky='nesw')

    hscroll.configure(command=self.msgBox.xview)
    vscroll.configure(command=self.msgBox.yview)


Then bind to all widgets to the event-function for Cntrl-C:

    self.bind_all('<Control-KeyPress-c>',self.Copy)

Where the Copy-function could look as follows:

  #-----------------------------------
  def Copy (self, *event):
  #-----------------------------------
    """
      Copies selected lines in the Listbox to Cilpboard

      @para event:            Dummy-parameter
      @type event:            list
      @return:                None
      @rtype:                 NN
    """
    if self.msgBox.size():
      cs=self.msgBox.curselection()
      if cs:
        # Overwirte the clipboardcontent
        self.msgBox.clipboard_clear()
        for lineno in cs:
          self.msgBox.clipboard_append(self.msgBox.get(lineno)+'\n')

I use the above example in the same app under W2kprof and HP-UX and
it works fine.

Regards
Peter



More information about the Python-list mailing list