実現したいこと
以下の流れのプログラムを作成したいです。
①長い処理を実行中に[実行中UI]を表示。
②一定時間で、継続するかのUIを表示させる。
③「はい」を押下時は、再度、[実行中UI]を表示。
「いいえ」の時は、終了。
④長い処理後は、[完了UI]を表示し終了。
以下の問題があります。
並行処理で実行中に実行中UIを出すのは、あるあるなので、簡単かとおもいましたが、
Tkinterやthreadingについて初心者のため、うまく動作しません。
お力沿いお願いします。
発生している問題と気になる問題。
①続きを実行しますか?で「いいえ」を押下しても終了しない。
②もっと、シンプルにならないか。。。
該当のソースコード
python3
1 2import sys 3import threading 4import tkinter as tk 5from tkinter import messagebox 6import time 7 8 9def Centering(window): 10 window.update_idletasks() 11 width = window.winfo_width() 12 height = window.winfo_height() 13 frm_width = window.winfo_rootx() - window.winfo_x() 14 win_width = width + 2 * frm_width 15 titlebar_height = window.winfo_rooty() -window.winfo_y() 16 win_height = height + titlebar_height + frm_width 17 x = window.winfo_screenwidth() // 2 - win_width // 2 18 y = window.winfo_screenheight() // 2 - win_height // 2 19 window.geometry('{}x{}+{}+{}'.format(width, height, x, y)) 20 window.deiconify() 21 22class GUI: 23 # お待ちくださいUI 24 def wait_ui(self): 25 self.running = True 26 self.window1 = tk.Tk() 27 self.window1.resizable(0,0) 28 self.window1.geometry("380x80") 29 self.window1.attributes('-alpha', 0.0) 30 Centering(self.window1) 31 self.window1.attributes('-alpha', 1.0, '-topmost', True) 32 self.window1.title("実行中...") 33 label1 = tk.Label(self.window1, text="ただいま実行中", font=("Meiryo",11)) 34 label1.pack() 35 label2 = tk.Label(self.window1, text="しばらくお待ちください", font=("Meiryo",11)) 36 label2.pack(pady = 10) 37 self.window1.after(1000, self._check_to_quit) 38 self.window1.protocol('WM_DELETE_WINDOW', self.quit) 39 self.window1.mainloop() 40 41 # 完了しましたUI 42 def generate_end(self): 43 self.window2 = tk.Tk() 44 self.window2.resizable(0,0) 45 self.window2.geometry("300x70") 46 self.window2.attributes('-alpha', 0.0) 47 Centering(self.window2) 48 self.window2.attributes('-alpha', 1.0, '-topmost', True) 49 self.window2.title("完了! ! !") 50 label1 = tk.Label(self.window2, text="完了しました", font=("Meiryo",11)) 51 label1.pack() 52 button = tk.Button(self.window2, text=u"OK", command=self.program_end) 53 button.pack() 54 self.window2.mainloop() 55 56 57 # Program終了関数 58 def _check_to_quit(self): 59 if self.running: 60 self.window1.after(1000, self._check_to_quit) 61 else: 62 self.window1.destroy() 63 def quit(self): 64 self.running = False 65 def program_end(self): 66 sys.exit() 67 68 69 70### メイン関数 ### 71def main(): 72 73 # 開始 74 gui = GUI() 75 thread1 = threading.Thread(target=gui.wait_ui) 76 thread1.start() 77 78 # 長い処理1 79 time.sleep(5) 80 81 gui.window1.attributes('-alpha', 0.0) # 透明化 82 83 ret = messagebox.askyesno('確認', '続きを実行しますか?') 84 if ret == False: 85 gui.window1.quit() 86 gui.window1.destroy() 87 sys.exit() 88 89 90 gui.window1.attributes('-alpha', 1.0, '-topmost', True) # 透明化解除 91 # 長い処理2 92 time.sleep(5) 93 94 # 終了 95 gui.quit() 96 thread1.join() 97 gui.generate_end() 98 99if __name__ == '__main__': 100 main()
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/12/13 11:57 編集
2022/12/13 13:40
2022/12/13 13:49