2

だから私は tkinter で個人的な使用のために非常に小さなプログラムを作っています、そして私は本当に奇妙な壁に遭遇しました. pywin32 の構文と命名規則に関係するすべてのことを本当に嫌うので、tkinter と pywin32 バインディングを混ぜ合わせています。奇妙なことは、pywin32 クリップボードの監視と tkinter での私のプログラムの反応との間の遷移で発生しています。

私のウィンドウとそのすべてのコントロールは tkinter で処理されています。pywin32 バインディングは、クリップボードが変更されたときに、クリップボードの監視とクリップボードへのアクセスを行っています。クリップボードが pywin32 の一部を監視する方法について私が収集したことから、ウィンドウの hwnd 値を pywin32 に提供する限り、何でも好きなように機能させることができます。私はその部分を行っており、プログラムが最初に起動したときに機能します。クリップボードが変更されたときに機能しないようです。

プログラムが起動すると、クリップボードが取得され、検索ボックスと編集ボックスに問題なく配置されます。クリップボードが変更されると、発生させたいイベントが発生します...プログラムが起動されたときに完全に機能していたイベントが、本来の動作ではなく奇妙なハングを引き起こしていることを除いて。クリップボードが変更された場合、クリップボードの内容を標準出力に出力できますが、同じデータを tkinter ウィジェットに入れることはできません。クリップボードの変更通知によって起動された後、tkinter ウィジェットのいずれかと対話し始めた場合にのみ、そのようにハングします。

私が使っていたクリップボード監視のサンプルコードを tkinter を使ったプログラムに適応させる際に、私が見逃した pywin32 のエチケットがあるように感じます。Tkinter はどうやらスタック トレースやエラー メッセージを生成するのを好まないようで、pdb でデバッグしようとして何を探すべきかを知ることさえできません。

コードは次のとおりです。

#coding: utf-8
#Clipboard watching cribbed from ## {{{ http://code.activestate.com/recipes/355593/ (r1)

import pdb
from Tkinter import *
import win32clipboard
import win32api
import win32gui
import win32con
import win32clipboard


def force_unicode(object, encoding="utf-8"):
    if isinstance(object, basestring) and not isinstance(object, unicode):
        object = unicode(object, encoding)
    return object

class Application(Frame):
    def __init__(self, master=None):
        self.master = master
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

        self.hwnd = self.winfo_id()
        self.nextWnd = None
        self.first = True
        self.oldWndProc = win32gui.SetWindowLong(self.hwnd, win32con.GWL_WNDPROC, self.MyWndProc)
        try:
            self.nextWnd = win32clipboard.SetClipboardViewer(self.hwnd)
        except win32api.error:
            if win32api.GetLastError () == 0:
                # information that there is no other window in chain
                pass
            else:
                raise

        self.update_search_box()
        self.word_search()

    def word_search(self):
        #pdb.set_trace()
        term = self.searchbox.get()
        self.resultsbox.insert(END, term)

    def update_search_box(self):
        clipboardtext = ""
        if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_TEXT):
            win32clipboard.OpenClipboard()
            clipboardtext = win32clipboard.GetClipboardData()
            win32clipboard.CloseClipboard()

        if clipboardtext != "":
            self.searchbox.delete(0,END)
            clipboardtext = force_unicode(clipboardtext)
            self.searchbox.insert(0, clipboardtext)

    def createWidgets(self):
        self.button = Button(self)
        self.button["text"] = "Search"
        self.button["command"] = self.word_search

        self.searchbox = Entry(self)
        self.resultsbox = Text(self)

        #Pack everything down here for "easy" layout changes later
        self.searchbox.pack()
        self.button.pack()
        self.resultsbox.pack()

    def MyWndProc (self, hWnd, msg, wParam, lParam):
        if msg == win32con.WM_CHANGECBCHAIN:
            self.OnChangeCBChain(msg, wParam, lParam)
        elif msg == win32con.WM_DRAWCLIPBOARD:
            self.OnDrawClipboard(msg, wParam, lParam)

        # Restore the old WndProc. Notice the use of win32api
        # instead of win32gui here. This is to avoid an error due to
        # not passing a callable object.
        if msg == win32con.WM_DESTROY:
            if self.nextWnd:
               win32clipboard.ChangeClipboardChain (self.hwnd, self.nextWnd)
            else:
               win32clipboard.ChangeClipboardChain (self.hwnd, 0)

            win32api.SetWindowLong(self.hwnd, win32con.GWL_WNDPROC, self.oldWndProc)

        # Pass all messages (in this case, yours may be different) on
        # to the original WndProc
        return win32gui.CallWindowProc(self.oldWndProc, hWnd, msg, wParam, lParam)

    def OnChangeCBChain (self, msg, wParam, lParam):
        if self.nextWnd == wParam:
           # repair the chain
           self.nextWnd = lParam
        if self.nextWnd:
           # pass the message to the next window in chain
           win32api.SendMessage (self.nextWnd, msg, wParam, lParam)

    def OnDrawClipboard (self, msg, wParam, lParam):
        if self.first:
           self.first = False
        else:
            #print "changed"
            self.word_search()
            #self.word_search()

        if self.nextWnd:
           # pass the message to the next window in chain
           win32api.SendMessage(self.nextWnd, msg, wParam, lParam)


if __name__ == "__main__":
    root = Tk()
    app = Application(master=root)
    app.mainloop()
    root.destroy()
4

1 に答える 1

2

それが役立つかどうかはわかりませんが、win32 イベントハンドラー内から更新を呼び出すと、tkinter がそれを気に入らない可能性があるため、故障すると思います。

これを回避するための通常のトリックは、 after_idle() コールバックを介して更新を延期することです。

だから置き換えてみてください:

   def OnDrawClipboard (self, msg, wParam, lParam):
    if self.first:
       self.first = False
    else:
        #print "changed"
        self.word_search()
        #self.word_search()

    if self.nextWnd:
       # pass the message to the next window in chain
       win32api.SendMessage(self.nextWnd, msg, wParam, lParam)

これとともに:

   def OnDrawClipboard (self, msg, wParam, lParam):
    if self.first:
       self.first = False
    else:
        #print "changed"
        self.after_idle(self.word_search)
        #self.word_search()

    if self.nextWnd:
       # pass the message to the next window in chain
       win32api.SendMessage(self.nextWnd, msg, wParam, lParam)
于 2011-01-06T00:00:25.403 に答える