質問

I am implementing a clipboard monitor in my python app. If the copied text meets certain requirements, i want to show the user a dialog. if the user clicks "Yes" i'd like to trigger a function - in this case webui.app.add_internal(cb) . If the user chooses "No" i'd like to just keep checking the clipboard for changes on 3 seconds intervals.

Everything seems to be working fine, but this code does not run repeatedly nonstop like I expect. investigating it a little shows that Tk().withdraw() seems to be the problematic line. Although without it the main blank window of Tk will appear next to the dialog shown, and the last choice of the user seems to be remembered for later (which isn't desired).

Consider the following code:

import pyperclip
import tkMessageBox, Tkinter
import threading

cbOld = "notAurl"
def catchClipboardChange():
global cbOld
cb = pyperclip.paste()
Tkinter.Tk().withdraw()
if (cb != cbOld):
    #Change in clipboard detected. Analize!
    if (urlValidation(cb) and isDownloadExt(cb) and tkMessageBox.askyesno("Start downloading?","Do you want to start downloading {}".format(cb))):
        webui.app.add_internal(cb)
    cbOld = cb # Old clipboard update
threading.Timer(3.0,catchClipboardChange).start()

catchClipboardChange();

Any suggestions how this can be done better? I was thinking either about how to show a dialog without the need for the withdraw methods, or safer ways to use it with threading. Appriciate any help

役に立ちましたか?

解決

No, you cannot call withdraw() from another thread other than the main thread.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top