Program termination problem

Eric Brunel eric.brunel at pragmadev.com
Fri Mar 29 04:47:04 EST 2002


Garyr wrote:
> I have a program that displays a Tkinter Text widget. I would like to be
> able to place text in the window via the clipboard. I found the following
> clipboard code in a Google post:
> 
> import win32clipboard as clip
>          ...
>         clip.OpenClipboard(0)
>         if clip.IsClipboardFormatAvailable(clip.CF_TEXT):
>            newText = clip.GetClipboardData(clip.CF_TEXT)
>           textwidget.insert(END, newText)
>         clip.CloseClipboard()
> 
> This works but it causes the program to hang on exit. mainloop() returns
> but the DOS window is frozen. The problem is associated with the clipboard
> functions; i.e., commenting the insert line has no effect.

I've already noticed that mixing Tkinter with calls to win32 functions 
often ends up with crashing or freezing programs. To do what you want to 
do, you'd better use the built-in clipboard management methods in Tkinter. 
With these methods, what you're trying to do may be written (untested code):

try:
  newText = yourTopLevelWindow.selection_get(selection='CLIPBOARD')
  textwidget.insert(END, newText)
except TclError:
  ## Nothing or incompatible data in clipboard
  pass

For copy operation, use the methods clipboard_clear and clipboard_append on 
your top-level window.

HTH
 - eric -




More information about the Python-list mailing list