Created
October 22, 2017 13:39
-
-
Save cryptogun/65ef5b8b8e1fe6905f87800c33baf9a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tkinter as tk | |
| import tkinter.messagebox as tkMessageBox | |
| import threading | |
| from queue import Queue | |
| # Thread-safe version. | |
| # Tkinter functions are put into queue and called by prompt_thread_loop | |
| # in the main thread. | |
| messagebox_queue = Queue() | |
| def prompt(title='', message='', message_type='INFO'): | |
| if message_type == 'INFO': | |
| messagebox_queue.put((tkMessageBox.showinfo, (title, message), {})) | |
| elif message_type == 'ERROR': | |
| messagebox_queue.put((tkMessageBox.showerror, (title, message), {})) | |
| def prompt_start_thread(func, args=(), kwargs={}): | |
| threading.Thread(target=func, args=args, kwargs=kwargs).start() | |
| def prompt_thread_loop(master): | |
| try: | |
| while True: | |
| func, args, kwargs = messagebox_queue.get_nowait() | |
| func(*args, **kwargs) | |
| except: | |
| pass | |
| master.after(100, lambda: prompt_thread_loop(master)) | |
| if __name__ == '__main__': | |
| root = tk.Tk() | |
| tkMessageBox.showinfo(message='blocking prompt') | |
| prompt_thread_loop(root) # prompt_thread_loop is launched here | |
| prompt_start_thread(lambda: prompt(message='non blocking prompt')) | |
| label = tk.Label(root, text='code after the second prompt.') | |
| label.pack() | |
| root.mainloop() | |
| # 循环查询有无弹窗: | |
| # prompt_thread_loop(root) | |
| # 用法: | |
| # from view import prompt_start_thread, prompt, view | |
| # prompt_start_thread(lambda: prompt(message='asdf', master=view)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment