Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Tkinter
- import threading
- import subprocess
- import time
- import win32gui
- import win32con
- def main_program():
- get_user_input()
- before = find_open_windows()
- launch_app_thread = threading.Thread(
- target=launch_software)
- launch_app_thread.start()
- button_thread = threading.Thread(
- target=InterruptButton)
- button_thread.start()
- wait_for_event()
- after = find_open_windows()
- close_recently_opened_windows(before, after)
- def find_open_windows():
- list_of_open_windows = []
- win32gui.EnumWindows(
- lambda hwnd,
- resultList: resultList.append(hwnd),
- list_of_open_windows)
- return list_of_open_windows
- def close_recently_opened_windows(before, after):
- for handle in after:
- if handle not in before:
- try:
- win32gui.PostMessage(handle, win32con.WM_CLOSE, 0, 0)
- except:
- pass
- time.sleep(.1)
- def wait_for_event():
- print 'waiting for something important to happen'
- while not important_event:
- time.sleep(2)
- class InterruptButton(threading.Thread):
- def __init__(self):
- threading.Thread.__init__(self)
- self.window = Tkinter.Tk()
- self.window.geometry('+0+0')
- self.window.overrideredirect(1)
- self.button = Tkinter.Button(
- self.window, text='Interrupt', command=self.interrupt)
- self.button.configure(background='red')
- self.button.pack()
- self.window.mainloop()
- def interrupt(self):
- print 'manually interrupted'
- global important_event
- important_event = True
- print 'now', important_event
- self.quit()
- def quit(self):
- self.window.quit()
- def launch_software(test_program='notepad.exe', test_command='test.txt'):
- subprocess.Popen([test_program, test_command])
- def get_user_input():
- popup = GetUserInput()
- if popup.command():
- return popup.command()
- class GetUserInput:
- def __init__(self):
- self.master = Tkinter.Toplevel()
- self.label = Tkinter.Label(self.master)
- self.label.pack()
- self.entry = Tkinter.Entry(self.master)
- self.entry.pack()
- self.button = Tkinter.Button(
- self.master, text="OK", command=self.command,
- default=Tkinter.ACTIVE)
- self.button.pack()
- self.master.mainloop()
- def command(self, event=None):
- y = self.entry.get()
- self.master.withdraw()
- self.master.quit()
- return y
- if __name__ == "__main__":
- root = Tkinter.Tk()
- root.withdraw()
- #important_event = False
- #main_program()
- while True:
- important_event = False
- main_program()
Advertisement
Add Comment
Please, Sign In to add comment