Using Tkinter To Watch For Clipboard,what's Wrong With My Code?
Solution 1:
I think multithreading isn't the best approach for your situation and after
may be enough. Instead of a busy waiting loop, you can call tk.after
with a lower timeout. Then you just need to move the logic of watch_clipboard
to your class, so you don't have to worry about the communication between the threads.
classGUI:
def__init__(self):
self.tk = Tk()
self.tk.resizable(0, 0)
self.tk.title('watch clipboard')
self.last_content = ''
self.tk.after(100, self.watch_clipboard)
self.tk.mainloop()
defwatch_clipboard(self):
try:
content = self.tk.clipboard_get()
if content != self.last_content and content.startswith('http:'):
self.last_content = content
self.tk.clipboard_clear()
messagebox.askokcancel('', 'add this in?', default='ok')
except TclError:
pass
self.tk.after(100, self.watch_clipboard)
Solution 2:
@laike9m
The error with your code is that os.pipe is used.
Os.read() is a blocking function that will receive os.read() blocking once clipboard_confirm
is run. Causes the UI to get stuck.
This bug has nothing to do with after
and multithreading.
Solution 3:
There's a good chance the problem is related to using threads -- calling Tkinter functions from anywhere other than the main thread can cause problems.
Is there a reason you're using a thread rather than takingbadvantage of the built-in infinite loop (the event loop)? I would advise making use of after
to check the keyboard every second or so.
Post a Comment for "Using Tkinter To Watch For Clipboard,what's Wrong With My Code?"