class Application: APP_NAME = 'MicroCom' def __init__(self): self.window_manager = WindowManager() self.state = { 'muted': False, 'connectivity': False } self.audio_manager = AudioManager(self.state) def start(self): settings.load_configuration() settings.set_window_manager(self.window_manager) self.window_manager.start() self.audio_manager.start() app = QApplication(sys.argv) main_window = MainWindow() if not QSystemTrayIcon.isSystemTrayAvailable(): QMessageBox.critical(None, self.APP_NAME, "I couldn't detect any system tray on this system.") sys.exit(1) app.setQuitOnLastWindowClosed(False) window = TrayWindow(main_window) sys.exit(app.exec_())
def __init__(self): self.window_manager = WindowManager() self.state = { 'muted': False, 'connectivity': False } self.audio_manager = AudioManager(self.state)
def __init__(self): """ initialization """ if len(sys.argv) <= 1: print("Usage: typhon.py filename") exit(255) filename = sys.argv[1] if not os.path.isfile(filename): print("File '" + filename + "' not found") exit(254) self._windowManager = WindowManager('Typhon', self.on_keypress) self._captureManager = CaptureManager(cv2.VideoCapture(filename), self._windowManager) self.current_frame = None self._draw_info = True self._draw_help = False self.paused = True self._ocr = False
def __init__(self, window): ''' Constructor ''' self.frame_no = 0 self.wm = WindowManager() self.view = SimulationView(self.wm) self.state = 'menu' self.window = window logging.debug('The state is {}'.format(self.state)) self.start_menu()
def __init__(self): """Base class initializer. Initialize base class and manager classes. Manager classes and their methods form the Driftwood Scripting API. Attributes: config: ConfigManager instance. log: LogManager instance. database: DatabaseManager instance. filetype: Shortcut to filetype module. tick: TickManager instance. path: PathManager instance. cache: CacheManager instance. resource: ResourceManager instance. input: InputManager instance. window: WindowManager instance. entity: EntityManager instance. area: AreaManager instance. script: ScriptManager instance. keycode: Contains the SDL keycodes. running: Whether the mainloop should continue running. Set False to shut down the engine. """ self.config = ConfigManager(self) self.log = LogManager(self) self.database = DatabaseManager(self) self.tick = TickManager(self) self.path = PathManager(self) self.cache = CacheManager(self) self.resource = ResourceManager(self) self.input = InputManager(self) self.window = WindowManager(self) self.entity = EntityManager(self) self.area = AreaManager(self) self.script = ScriptManager(self) # SDL Keycodes. self.keycode = keycode self.running = False
def clone(self): WindowManager().create(self.current_window.config)
class TyphonApp(object): def __init__(self): """ initialization """ if len(sys.argv) <= 1: print("Usage: typhon.py filename") exit(255) filename = sys.argv[1] if not os.path.isfile(filename): print("File '" + filename + "' not found") exit(254) self._windowManager = WindowManager('Typhon', self.on_keypress) self._captureManager = CaptureManager(cv2.VideoCapture(filename), self._windowManager) self.current_frame = None self._draw_info = True self._draw_help = False self.paused = True self._ocr = False def run(self): self._windowManager.create_window() """ Main loop """ while self._windowManager.is_window_created: self._captureManager.enter_frame() frame = self._captureManager.frame # BEGIN self.current_frame = self._captureManager.frames_elapsed ######################################################### # frame = self.rotate_image(frame, 45) if self._ocr: self.image_to_text(frame) self._ocr = False if self._draw_info: txt = "Frame: " + str(self.current_frame) + "/" + \ str(self._captureManager.total_frames) self.draw_text(frame, txt, (10, 60), (255,) * 3) if not self._draw_help: self.draw_text(frame, 'Press [h] for help', (10, 100), (255,) * 3) if self._draw_help: self.draw_help(frame) # END self._captureManager.exit_frame() self._windowManager.process_events(self.paused) # def mouse_event(self, event, x, y, flags, param): # print ("Mouse event :", event, x, y, flags, param) # return def on_keypress(self, keycode): # print('Keycode #', keycode) if keycode == 27: # ESC, quit self._windowManager.destroy_window() elif keycode == ord('p'): self.paused = not self.paused elif keycode == ord('h'): self._draw_help = not self._draw_help elif keycode == ord('t'): self._ocr = True elif keycode == ord('s'): self._captureManager.write_image('screenshot.png') elif keycode == ord('i'): self._draw_info = not self._draw_info elif keycode == ord('b'): # go back 50 frames self._captureManager.frames_elapsed -= 51 elif keycode == ord('n'): # forward 50 frames self._captureManager.frames_elapsed += 49 elif keycode == 81: # go back 5 frames self._captureManager.frames_elapsed -= 6 elif keycode == 83: # forward 5 frames self._captureManager.frames_elapsed += 4 elif 48 <= keycode <= 57: print(self._captureManager.fps) a = ((keycode - 48) * 100) print('Delay:', a) if a == 0: a = 1 self._windowManager.set_delay(int(a)) def draw_help(self, frame): sy = 200 color = (0, 255, 128) # lime txt = "====== Help ======" self.draw_text(frame, txt, (10, sy), color) txt = "[ESC] Quit" self.draw_text(frame, txt, (10, sy + 25), color) txt = "[p] Pause mode " + ("ON" if self.paused else "OFF") self.draw_text(frame, txt, (10, sy + 50), color) txt = "[t] OCR and print result" self.draw_text(frame, txt, (10, sy + 75), color) txt = "[b]/[n] REW/FFD 50 frames" self.draw_text(frame, txt, (10, sy + 100), color) txt = "[LEFT]/[RIGHT] REW/FFD 5 frames" self.draw_text(frame, txt, (10, sy + 125), color) txt = "[s] Save screenshot" self.draw_text(frame, txt, (10, sy + 150), color) txt = "[0]-[9] Set delay" self.draw_text(frame, txt, (10, sy + 175), color) def outline_rect(self, image, rect, color, thickness=1): # color is in BGR # e.g. outlineRect(frame, (10, 10, 400, 200), (255, 128, 0)) if rect is None: return x, y, w, h = rect cv2.rectangle(image, (x, y), (x + w, y + h), color, thickness) def rotate_image(self, image, angle): center = tuple(np.array(image.shape[0:2]) / 2) rot_mat = cv2.getRotationMatrix2D(center, angle, 1.0) return cv2.warpAffine(image, rot_mat, image.shape[0:2], flags=cv2.INTER_LINEAR) def draw_text(self, frame, text, pos, color): cv2.putText(frame, text, pos, cv2.FONT_HERSHEY_SIMPLEX, .5, (0,) * 3, 2, cv2.CV_AA) cv2.putText(frame, text, pos, cv2.FONT_HERSHEY_SIMPLEX, .5, color, 1, cv2.CV_AA) def image_to_text(self, img): a = np.asarray(img) i = Image.fromarray(a) print(pytesseract.image_to_string(i))
def __init__(self, device): self._device = device self._activity_manager = ActivityManager.get_instance(device) self._window_manager = WindowManager.get_instance(device) self._driver_dict = {}
class Application: ''' classdocs ''' def __init__(self, window): ''' Constructor ''' self.frame_no = 0 self.wm = WindowManager() self.view = SimulationView(self.wm) self.state = 'menu' self.window = window logging.debug('The state is {}'.format(self.state)) self.start_menu() def update(self, dt): self.view.update(dt) self.frame_no += 1 def on_draw(self): gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.view.draw() self.wm.draw() def on_mouse_press(self, x, y, *args): hit = self.wm.on_mouse_press(x, self.window.height - y, *args) if not hit and self.state == 'simu': self.view.on_mouse_press(x, self.window.height - y, *args) def show_menu(self): self.menu = Window(self.wm.root, 100, 100, 440, 280) Label(self.menu, 'OpenPark Main Menu', 10, 10) Label(self.menu, 'Leonhard Vogt 2015', 10, 40) Label(self.menu, '[ new empty simulation ]', 10, 100).on_click = self.menu_new Label(self.menu, '[ quit program ]', 10, 180).on_click = self.menu_quit if os.path.exists(self.autofile()): Label(self.menu, '[ load last game ]', 10, 130).on_click = lambda:self.load_simulation(self.autofile()) def new_empty_simulation(self): self.menu.close() simu = Simulation(16, 16) self.view.unload() self.view.load(simu) def start_menu(self): # simu = load_background_simulation() self.view.unload() # self.view.load(simu) self.show_menu() def menu_new(self): self.state = 'simu' logging.debug('The state is {}'.format(self.state)) self.new_empty_simulation() def menu_quit(self): pyglet.app.exit() def on_key_press(self, symbol, modifiers): logging.debug('Key Press {} {}'.format(symbol, modifiers)) if symbol == key.I: logging.info('FPS: {}'.format(pyglet.clock.get_fps())) logging.info('Frame-No: {}'.format(self.frame_no)) self.wm.textmanager.dump() if self.state == 'menu': if symbol == key.N: self.menu_new() if symbol == key.Q: self.menu_quit() elif self.state == 'simu': if symbol == key.ESCAPE: return pyglet.event.EVENT_HANDLED if symbol == key.Q: self.autosave() self.start_menu() self.state = 'menu' logging.debug('The state is {}'.format(self.state)) if symbol == key.S: self.autosave() self.view.on_key_press(symbol, modifiers) else: assert False def on_mouse_motion(self, x, y, dx, dy): if self.state == 'simu': self.view.on_mouse_motion(x, y, dx, dy) def on_mouse_leave(self, x, y): if self.state == 'simu': self.view.on_mouse_leave(x, y) def on_mouse_drag(self, *args): if self.state == 'simu': self.view.on_mouse_drag(*args) def on_resize(self, x, y): logging.info('Window Resized to {}x{}'.format(x, y)) x = max(x, 1) y = max(y, 1) gl.glViewport(0, 0, x, y) self.wm.on_resize(x, y) self.view.on_resize(x, y) def load_simulation(self, filename): self.view.unload() with open(filename, 'rt', encoding='utf8') as fp: simulation = Simulation.deserialize(json.load(fp)) self.view.load(simulation) self.state = 'simu' def save_simulation(self, filename): if not os.path.isdir(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) with open(filename, 'w', encoding='ascii') as fp: json.dump(self.view.simulation.serialize(), fp) logging.info('Saved {}'.format(os.path.abspath(filename))) def autofile(self): return os.path.expanduser('~/.openpark/autosave.json') def autosave(self): if self.state == 'simu': self.save_simulation(self.autofile())
class Driftwood: """The top-level base class This class contains the top level manager class instances and the mainloop. The instance of this class is passed to scripts as an API reference. """ def __init__(self): """Base class initializer. Initialize base class and manager classes. Manager classes and their methods form the Driftwood Scripting API. Attributes: config: ConfigManager instance. log: LogManager instance. database: DatabaseManager instance. filetype: Shortcut to filetype module. tick: TickManager instance. path: PathManager instance. cache: CacheManager instance. resource: ResourceManager instance. input: InputManager instance. window: WindowManager instance. entity: EntityManager instance. area: AreaManager instance. script: ScriptManager instance. keycode: Contains the SDL keycodes. running: Whether the mainloop should continue running. Set False to shut down the engine. """ self.config = ConfigManager(self) self.log = LogManager(self) self.database = DatabaseManager(self) self.tick = TickManager(self) self.path = PathManager(self) self.cache = CacheManager(self) self.resource = ResourceManager(self) self.input = InputManager(self) self.window = WindowManager(self) self.entity = EntityManager(self) self.area = AreaManager(self) self.script = ScriptManager(self) # SDL Keycodes. self.keycode = keycode self.running = False def run(self): """Perform startup procedures and enter the mainloop. """ # Only run if not already running. if not self.running: self.running = True # Execute the init function of the init script if present. if not self.path["init.py"]: self.log.msg("WARNING", "Driftwood", "init.py missing, nothing will happen") else: self.script.call("init.py", "init") # Escape key pauses the engine. self.input.register(self.keycode.SDLK_ESCAPE, self.__handle_pause) # This is the mainloop. while self.running: # Process SDL events. sdlevents = sdl2ext.get_events() for event in sdlevents: if event.type == SDL_QUIT: # Stop running. self.running = False elif event.type == SDL_KEYDOWN: # Pass a keydown to the Input Manager. self.input._key_down(event.key.keysym.sym) elif event.type == SDL_KEYUP: # Pass a keyup to the Input Manager. self.input._key_up(event.key.keysym.sym) elif event.type == SDL_WINDOWEVENT and event.window.event == SDL_WINDOWEVENT_EXPOSED: self.window.refresh() # Process tick callbacks. self.tick.tick() print("Shutting down...") return 0 def __handle_pause(self, keyevent): if keyevent == InputManager.ONDOWN: # Shift+Escape shuts down the engine. if self.input.pressed(self.keycode.SDLK_LSHIFT) or self.input.pressed(self.keycode.SDLK_RSHIFT): self.running = False else: self.tick.toggle_pause()
import tkinter as tk from windowmanager import WindowManager root = tk.Tk() # root.withdraw() # initialise WindowManager win_man = WindowManager(root) # Create a new window win_man.new_window("testpopup") # Adding items to new window testwindow = win_man.window_dict["testpopup"][1] button = tk.Button(testwindow, text="test text here", command=lambda c="testpopup": win_man.close_window(c)) button.pack() root.mainloop()