目的
メインのプログラム実行中に、tkinterで作ったウインドウに非常停止ボタンを表示しておき、そのボタンが押されるとメインプログラムが停止するようにプログラムを作っています。
この作ったtkinterのウインドウを常にウインドウ前面に表示させるようにしたいです。
[自分でやったこと]
メインのプロセスとは別に非常停止ボタンをtkinterで作ることに対しては、from multiprocessing import ProcessとしてProcessでtkinterでウインドウを作る関数を呼び出すことで実現しています。ですから、tkinterをウインドウを作る関数を呼び出した後、ctypes.windll.user32.GetForegroundWindow()でwindowIDを取得してファイルに記述。
それから、tkinterで作ったウインドウIDのウインドウを0.2秒毎にctypes.windll.user32.SetForegroundWindowでフロントに移動させる関数を別にProcessで独立に動かしメインプロセスを動かしている最中に非常停止ボタンが常に前面に居るようにしようとしました。ですがうまく行きません。コードは以下のとおりです。
Python
1 2from multiprocessing import Process 3from time import sleep 4import tkinter 5import ctypes 6import os, signal 7 8def StopBottun(): 9 root = tkinter.Tk() 10 label = tkinter.Label(root, text='StopBottun') 11 label.pack() 12 button1 = tkinter.Button(root, text='Exit', command=lambda: os.kill(os.getppid(), signal.SIGTERM)) 13 button1.pack() 14 root.mainloop() 15 16def MoveFront(): 17 with open(r"StopBottunwindowID.txt", 'r', encoding='utf-8') as file: 18 WindowID = file.read() 19 while True: 20 ctypes.windll.user32.SetForegroundWindow(WindowID) 21 sleep(0.2) 22 23 24 25def main(): 26 i1 = Process (target=StopBottun) 27 i1.start() 28 hwnd = ctypes.windll.user32.GetForegroundWindow() 29 with open(r"StopBottunwindowID.txt", 'w', encoding='utf-8') as file: 30 file.write(str(hwnd)) 31 sleep(0.5) 32 i2 = Process (target=MoveFront) 33 i2.start() 34 35 ここから先でメインプログラムが流れる 36 37 38 39 40if __name__ == "__main__": 41 main() 42
どなたか、教えて頂けると非常にありがたいです。
GUI作ったと無くて試行錯誤しているのですが、なかなか非常停止ボタンの実装が出来ません。
宜しくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/04 09:37