def __init__(self, parent=None, width=5, height=4, dpi=100): super().__init__(parent) fig = Figure(figsize=(width, height), dpi=dpi) ax = Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) self.ax = ax self.fig = fig self.canvas = FigureCanvas(fig) self.toolbar = NavigationToolbar(self.canvas, self, False) layout = QtWidgets.QVBoxLayout() layout.addWidget(self.toolbar) layout.addWidget(self.canvas) self.setLayout(layout) self.fig.canvas.mpl_connect('button_press_event', self._on_press) self.fig.canvas.mpl_connect('button_release_event', self._on_release) self.fig.canvas.mpl_connect('motion_notify_event', self._on_motion) self.crop_x0 = None self.crop_x1 = None self.crop_y0 = None self.crop_y1 = None self.bad_flag = False
def show_Ulam_spiral(Ulam_data): """ Plots the Ulam spiral and saves it. Depends on matplotlib.pyplot """ from matplotlib.pyplot import set_cmap,savefig,figure,Axes,show fig = figure() fig.set_size_inches(6, 6) ax = Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) # set_cmap('binary') ax.imshow(Ulam_data, interpolation='nearest') show()
def wav_to_spectrogram(audio_path, save_path, spectrogram_dimensions=(64, 64), noverlap=16, cmap='gray_r'): data = load_audio(audio_path, mono=True)[0] fig = figure() fig.set_size_inches((spectrogram_dimensions[0] / fig.get_dpi(), spectrogram_dimensions[1] / fig.get_dpi())) ax = Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) ax.specgram(data, cmap=cmap, Fs=2, noverlap=noverlap) ax.xaxis.set_major_locator(NullLocator()) ax.yaxis.set_major_locator(NullLocator()) fig.savefig(save_path, bbox_inches="tight", pad_inches=0)
def __init__(self, parent=None, width=5, height=4, dpi=100): fig = Figure(figsize=(width, height), dpi=dpi) ax = Axes(fig, [0., 0., 1., 1.]) fig.add_axes(ax) self.axes = ax self.fig = fig self.canvas = FigureCanvas.__init__(self, fig) self.setParent(parent) FigureCanvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) FigureCanvas.updateGeometry(self) #self.figure.subplots_adjust(left= 0.15, bottom=0.15) window_brush = self.window().palette().window() fig.set_facecolor(brush_to_color_tuple(window_brush)) fig.set_facecolor(brush_to_color_tuple(window_brush)) self.reset()
def __init__(self, parent=None, width=5, height=4, dpi=100): super().__init__(parent) fig = Figure(figsize=(width, height), dpi=dpi) ax = Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) self.ax = ax self.fig = fig self.canvas = FigureCanvas(fig) # initialized by _get_roi_bar() self.sp_x0 = None self.sp_y0 = None self.sp_w = None self.sp_h = None self._roi_all = None self.ref_roi_side = [64, 96, 128, 160, 192, 224, 256] # x 32, square self._actions = {} self._active = None self._eventHandler = EventHandler() self.roi_changed = self._eventHandler.roi_changed self._ids = [] layout = QtWidgets.QVBoxLayout() layout.addWidget(self.canvas) layout.addLayout(self._get_toolbar()) layout.addLayout(self._get_roi_bar()) self.setLayout(layout) self._eventHandler.brush_changed.connect(self.update_overlay) self.image = None self.image_data = None self.image_handler = None self.overlay = None self.overlay_handler = None self.reset()
def init(self): r"""Initializes the GUI.""" #%% properties QToolTip.setFont(QtGui.QFont('SansSerif', 10)) # Central Widget self.gui_widget = QWidget(self) self.setCentralWidget(self.gui_widget) # Panels self.grp_score = QWidget() self.grp_move = QWidget() self.grp_buttons = QWidget() self.grp_board = QWidget() self.grp_main = QWidget() #%% Layouts layout_gui = QVBoxLayout(self.gui_widget) layout_main = QHBoxLayout(self.grp_main) layout_board = QVBoxLayout(self.grp_board) layout_buttons = QHBoxLayout(self.grp_buttons) layout_score = QGridLayout(self.grp_score) layout_move = QVBoxLayout(self.grp_move) for layout in [ layout_gui, layout_main, layout_board, layout_buttons, layout_score, layout_move ]: layout.setAlignment(QtCore.Qt.AlignCenter) #%% Text # Tic Tac Toe lbl_tictactoe = QLabel('Tic Tac Toe') lbl_tictactoe.setStyleSheet('font-size: 18pt; font: bold;') lbl_tictactoe.setMinimumWidth(220) # Score lbl_score = QLabel('Score:') lbl_score.setStyleSheet('font-size: 12pt; font: bold;') lbl_score.setMinimumWidth(220) # Move lbl_move = QLabel('Move:') lbl_move.setStyleSheet('font-size: 12pt; font: bold;') lbl_move.setMinimumWidth(220) # O Wins lbl_o = QLabel('O Wins:') lbl_o.setMinimumWidth(80) # X Wins lbl_x = QLabel('X Wins:') lbl_x.setMinimumWidth(80) # Games Tied lbl_games = QLabel('Games Tied:') lbl_games.setMinimumWidth(80) for label in [ lbl_tictactoe, lbl_score, lbl_move, lbl_o, lbl_x, lbl_games ]: label.setAlignment(QtCore.Qt.AlignCenter) # Changeable labels self.lbl_o_wins = QLabel('0') self.lbl_x_wins = QLabel('0') self.lbl_games_tied = QLabel('0') for label in [self.lbl_o_wins, self.lbl_x_wins, self.lbl_games_tied]: label.setAlignment(QtCore.Qt.AlignRight) label.setMinimumWidth(60) #%% Axes # board fig = Figure(figsize=(4.2, 4.2), dpi=100, frameon=False) self.board_canvas = FigureCanvas(fig) self.board_canvas.mpl_connect( 'button_release_event', lambda event: self.mouse_click_callback(event)) self.board_canvas.setMinimumSize(420, 420) self.board_axes = Axes(fig, [0., 0., 1., 1.]) self.board_axes.invert_yaxis() fig.add_axes(self.board_axes) # current move fig = Figure(figsize=(1.05, 1.05), dpi=100, frameon=False) self.move_canvas = FigureCanvas(fig) self.move_canvas.setMinimumSize(105, 105) self.move_axes = Axes(fig, [0., 0., 1., 1.]) self.move_axes.set_xlim(-SIZES['square'] / 2, SIZES['square'] / 2) self.move_axes.set_ylim(-SIZES['square'] / 2, SIZES['square'] / 2) self.move_axes.set_axis_off() fig.add_axes(self.move_axes) #%% Buttons # Undo button self.btn_undo = QPushButton('Undo') self.btn_undo.setToolTip('Undoes the last move.') self.btn_undo.setMinimumSize(60, 30) self.btn_undo.setStyleSheet( 'color: yellow; background-color: #990000; font: bold;') self.btn_undo.clicked.connect(self.btn_undo_function) # New Game button self.btn_new = QPushButton('New Game') self.btn_new.setToolTip('Starts a new game.') self.btn_new.setMinimumSize(60, 50) self.btn_new.setStyleSheet( 'color: yellow; background-color: #006633; font: bold;') self.btn_new.clicked.connect(self.btn_new_function) # Redo button self.btn_redo = QPushButton('Redo') self.btn_redo.setToolTip('Redoes the last move.') self.btn_redo.setMinimumSize(60, 30) self.btn_redo.setStyleSheet( 'color: yellow; background-color: #000099; font: bold;') self.btn_redo.clicked.connect(self.btn_redo_function) for btn in [self.btn_undo, self.btn_new, self.btn_redo]: not_resize = btn.sizePolicy() not_resize.setRetainSizeWhenHidden(True) btn.setSizePolicy(not_resize) #%% Populate Widgets # score layout_score.addWidget(lbl_score, 0, 0, 1, 2) layout_score.addWidget(lbl_o, 1, 0) layout_score.addWidget(self.lbl_o_wins, 1, 1) layout_score.addWidget(lbl_x, 2, 0) layout_score.addWidget(self.lbl_x_wins, 2, 1) layout_score.addWidget(lbl_games, 3, 0) layout_score.addWidget(self.lbl_games_tied) # move layout_move.addWidget(lbl_move) layout_move.addWidget(self.move_canvas) # buttons layout_buttons.addWidget(self.btn_undo) layout_buttons.addWidget(self.btn_new) layout_buttons.addWidget(self.btn_redo) # board layout_board.addWidget(self.board_canvas) layout_board.addWidget(self.grp_buttons) # main layout_main.addWidget(self.grp_score) layout_main.addWidget(self.grp_board) layout_main.addWidget(self.grp_move) # main GUI layout_gui.addWidget(lbl_tictactoe) layout_gui.addWidget(self.grp_main) #%% File Menu # actions - new game act_new_game = QAction('New Game', self) act_new_game.setShortcut('Ctrl+N') act_new_game.setStatusTip('Starts a new game.') act_new_game.triggered.connect(self.act_new_game_func) # actions - options act_options = QAction('Options', self) act_options.setShortcut('Ctrl+O') act_options.setStatusTip('Opens the advanced option settings.') act_options.triggered.connect(self.act_options_func) # actions - quit game act_quit = QAction('Exit', self) act_quit.setShortcut('Ctrl+Q') act_quit.setStatusTip('Exits the application.') act_quit.triggered.connect(self.close) # menubar self.statusBar() menu_bar = self.menuBar() file_menu = menu_bar.addMenu('&File') file_menu.addAction(act_new_game) file_menu.addAction(act_options) file_menu.addAction(act_quit) #%% Finalization # Call wrapper to initialize GUI self.wrapper() # GUI final layout properties self.center() self.setWindowTitle('Tic Tac Toe') self.setWindowIcon( QtGui.QIcon(os.path.join(get_images_dir(), 'tictactoe.png'))) self.show()
def init(self): r"""Initializes the GUI.""" #%% properties QToolTip.setFont(QtGui.QFont('SansSerif', 10)) #%% Text # Pentago lbl_pentago = QLabel('Pentago', self) lbl_pentago.setGeometry(390, 50, 220, 40) lbl_pentago.setAlignment(QtCore.Qt.AlignCenter) lbl_pentago.setStyleSheet('font-size: 18pt; font: bold;') # Score lbl_score = QLabel('Score:', self) lbl_score.setGeometry(35, 220, 220, 40) lbl_score.setAlignment(QtCore.Qt.AlignCenter) lbl_score.setStyleSheet('font-size: 12pt; font: bold;') # Move lbl_move = QLabel('Move:', self) lbl_move.setGeometry(725, 220, 220, 40) lbl_move.setAlignment(QtCore.Qt.AlignCenter) lbl_move.setStyleSheet('font-size: 12pt; font: bold;') # White Wins lbl_white = QLabel('White Wins:', self) lbl_white.setGeometry(80, 280, 80, 20) # Black Wins lbl_black = QLabel('Black Wins:', self) lbl_black.setGeometry(80, 310, 80, 20) # Games Tied lbl_games = QLabel('Games Tied:', self) lbl_games.setGeometry(80, 340, 80, 20) # Changeable labels self.lbl_white_wins = QLabel('0', self) self.lbl_white_wins.setGeometry(140, 280, 60, 20) self.lbl_white_wins.setAlignment(QtCore.Qt.AlignRight) self.lbl_black_wins = QLabel('0', self) self.lbl_black_wins.setGeometry(140, 310, 60, 20) self.lbl_black_wins.setAlignment(QtCore.Qt.AlignRight) self.lbl_games_tied = QLabel('0', self) self.lbl_games_tied.setGeometry(140, 340, 60, 20) self.lbl_games_tied.setAlignment(QtCore.Qt.AlignRight) #%% Axes # board self.wid_board = QWidget(self) self.wid_board.setGeometry(290, 140, 420, 420) fig = Figure(figsize=(4.2, 4.2), dpi=100, frameon=False) self.board_canvas = FigureCanvas(fig) self.board_canvas.setParent(self.wid_board) self.board_canvas.mpl_connect( 'button_release_event', lambda event: self.mouse_click_callback(event)) self.board_axes = Axes(fig, [0., 0., 1., 1.]) self.board_axes.invert_yaxis() #self.board_axes.set_axis_off() # TODO: do I need this line, or handled in self.setup_axes()? fig.add_axes(self.board_axes) # current move self.wid_move = QWidget(self) self.wid_move.setGeometry(800, 280, 70, 70) fig = Figure(figsize=(.7, .7), dpi=100, frameon=False) self.move_canvas = FigureCanvas(fig) self.move_canvas.setParent(self.wid_move) self.move_axes = Axes(fig, [0., 0., 1., 1.]) self.move_axes.set_xlim(-SIZES['square'] / 2, SIZES['square'] / 2) self.move_axes.set_ylim(-SIZES['square'] / 2, SIZES['square'] / 2) self.move_axes.set_axis_off() fig.add_axes(self.move_axes) #%% Buttons button_size = QtCore.QSize(SIZES['button'], SIZES['button']) # Undo button self.btn_undo = QPushButton('Undo', self) self.btn_undo.setToolTip('Undoes the last move.') self.btn_undo.setGeometry(380, 600, 60, 30) self.btn_undo.setStyleSheet( 'color: yellow; background-color: #990000; font: bold;') self.btn_undo.clicked.connect(self.btn_undo_function) # New Game button self.btn_new = QPushButton('New Game', self) self.btn_new.setToolTip('Starts a new game.') self.btn_new.setGeometry(460, 600, 80, 50) self.btn_new.setStyleSheet( 'color: yellow; background-color: #006633; font: bold;') self.btn_new.clicked.connect(self.btn_new_function) # Redo button self.btn_redo = QPushButton('Redo', self) self.btn_redo.setToolTip('Redoes the last move.') self.btn_redo.setGeometry(560, 600, 60, 30) self.btn_redo.setStyleSheet( 'color: yellow; background-color: #000099; font: bold;') self.btn_redo.clicked.connect(self.btn_redo_function) # 1R button self.btn_1R = RotationButton('', self, quadrant=1, direction=1) self.btn_1R.setToolTip('Rotates quadrant 1 to the right 90 degrees.') self.btn_1R.setIconSize(button_size) self.btn_1R.setGeometry(290, 49, SIZES['button'], SIZES['button']) self.btn_1R.clicked.connect(self.btn_rot_function) # 2R button self.btn_2R = RotationButton('', self, quadrant=2, direction=1) self.btn_2R.setToolTip('Rotates quadrant 2 to the right 90 degrees.') self.btn_2R.setIconSize(button_size) self.btn_2R.setGeometry(730, 139, SIZES['button'], SIZES['button']) self.btn_2R.clicked.connect(self.btn_rot_function) # 3R button self.btn_3R = RotationButton('', self, quadrant=3, direction=1) self.btn_3R.setToolTip('Rotates quadrant 3 to the right 90 degrees.') self.btn_3R.setIconSize(button_size) self.btn_3R.setGeometry(200, 489, SIZES['button'], SIZES['button']) self.btn_3R.clicked.connect(self.btn_rot_function) # 4R button self.btn_4R = RotationButton('', self, quadrant=4, direction=1) self.btn_4R.setToolTip('Rotates quadrant 4 to the right 90 degrees.') self.btn_4R.setIconSize(button_size) self.btn_4R.setGeometry(640, 579, SIZES['button'], SIZES['button']) self.btn_4R.clicked.connect(self.btn_rot_function) # 1L button self.btn_1L = RotationButton('', self, quadrant=1, direction=-1) self.btn_1L.setToolTip('Rotates quadrant 1 to the left 90 degrees.') self.btn_1L.setIconSize(button_size) self.btn_1L.setGeometry(200, 139, SIZES['button'], SIZES['button']) self.btn_1L.clicked.connect(self.btn_rot_function) # 2L button self.btn_2L = RotationButton('', self, quadrant=2, direction=-1) self.btn_2L.setToolTip('Rotates quadrant 2 to the left 90 degrees.') self.btn_2L.setIconSize(button_size) self.btn_2L.setGeometry(640, 49, SIZES['button'], SIZES['button']) self.btn_2L.clicked.connect(self.btn_rot_function) # 3L button self.btn_3L = RotationButton('', self, quadrant=3, direction=-1) self.btn_3L.setToolTip('Rotates quadrant 3 to the left 90 degrees.') self.btn_3L.setIconSize(button_size) self.btn_3L.setGeometry(290, 579, SIZES['button'], SIZES['button']) self.btn_3L.clicked.connect(self.btn_rot_function) # 4L button self.btn_4L = RotationButton('', self, quadrant=4, direction=-1) self.btn_4L.setToolTip('Rotates quadrant 4 to the left 90 degrees.') self.btn_4L.setIconSize(button_size) self.btn_4L.setGeometry(730, 489, SIZES['button'], SIZES['button']) self.btn_4L.clicked.connect(self.btn_rot_function) # buttons dictionary for use later self.rot_buttons = {'1L':self.btn_1L, '2L':self.btn_2L, '3L':self.btn_3L, '4L':self.btn_4L, \ '1R':self.btn_1R, '2R':self.btn_2R, '3R':self.btn_3R, '4R':self.btn_4R} #%% Finalization # Call wrapper to initialize GUI self.wrapper() # GUI final layout properties self.resize(1000, 700) self.center() self.setWindowTitle('Pentago') self.setWindowIcon( QtGui.QIcon(os.path.join(get_images_dir(), 'pentago.png'))) self.show()
def init(self): r"""Initializes the GUI.""" # Check to see if the Default profile exists, and if so load it, else create it folder = get_root_dir() filename = os.path.join(folder, 'Default.pkl') if os.path.isfile(filename): # pragma: no cover self.gui_settings = GuiSettings.load(filename) else: # pragma: no cover self.gui_settings.save(filename) # initialize timer self.time = QtCore.QTimer(self) # properties QToolTip.setFont(QtGui.QFont('SanSerif', 10)) # alias some colors tile_color = colors.to_hex(COLOR['tile']) # Central Widget self.gui_widget = QWidget(self) self.setCentralWidget(self.gui_widget) # Panels self.grp_tiles = QWidget() self.grp_moves = QWidget() self.grp_left = QWidget() self.grp_center = QWidget() self.grp_right = QWidget() self.grp_main = QWidget() #%% Layouts layout_gui = QVBoxLayout(self.gui_widget) layout_main = QHBoxLayout(self.grp_main) layout_left = QVBoxLayout(self.grp_left) layout_center = QVBoxLayout(self.grp_center) layout_right = QVBoxLayout(self.grp_right) layout_tiles = QHBoxLayout(self.grp_tiles) layout_moves = QGridLayout(self.grp_moves) for layout in [layout_gui, layout_main, layout_left, layout_center, layout_right, layout_tiles, layout_moves]: layout.setAlignment(QtCore.Qt.AlignCenter) #%% Labels lbl_title = QLabel('Scrabble & Words With Friends Cheater') lbl_tile_bag = QLabel('Tile Bag') lbl_draw_stats = QLabel('Draw Stats') lbl_strength = QLabel('Move Strength') lbl_moves = QLabel('Best Moves') for label in [lbl_title, lbl_tile_bag, lbl_draw_stats, lbl_strength, lbl_moves]: label.setAlignment(QtCore.Qt.AlignCenter) #%% Text Edit Boxes self.lne_tile_bag = QLineEdit('') #%% Axes # board fig = Figure(figsize=(4.2, 4.2), dpi=100, frameon=False) self.board_canvas = FigureCanvas(fig) #self.board_canvas.setParent(self.grp_center) # TODO: layout instead? self.board_canvas.mpl_connect('button_release_event', lambda event: self.mouse_click_callback(event)) self.board_axes = Axes(fig, [0., 0., 1., 1.]) self.board_axes.invert_yaxis() fig.add_axes(self.board_axes) # draw stats fig = Figure(figsize=(2.2, 1.1), dpi=100, frameon=False) self.draw_stats_canvas = FigureCanvas(fig) #self.draw_stats_canvas.setParent(self.grp_left) # TODO: layout instead? self.draw_stats_axes = Axes(fig, [0., 0., 1., 1.]) fig.add_axes(self.draw_stats_axes) # move strength fig = Figure(figsize=(2.2, 2.2), dpi=100, frameon=False) self.strength_canvas = FigureCanvas(fig) #self.strength_canvas.setParent(self.grp_right) # TODO: layout instead? self.strength_axes = Axes(fig, [0., 0., 1., 1.]) fig.add_axes(self.strength_axes) #%% Buttons self.btn_play = QPushButton('PLAY') self.btn_play.setToolTip('Ploy the current move.') self.btn_play.setMaximumWidth(200) self.btn_play.setStyleSheet('color: black; background-color: #00bfbf; font: bold;') self.btn_play.clicked.connect(self.btn_play_func) for ix in range(7): temp = QPushButton('') temp.setMaximumWidth(20) temp.setStyleSheet(f'color: black; background-color: {tile_color};') temp.clicked.connect(lambda state, x=ix: self.btn_tile_func(x)) setattr(self, f'btn_tile{ix}', temp) for ix in range(10): temp = QPushButton('') temp.setMaximumWidth(20) temp.setStyleSheet(f'color: black; background-color: {tile_color};') temp.clicked.connect(lambda state, x=ix: self.btn_move_func(x)) setattr(self, f'btn_move{ix}', temp) #%% Populate Widgets # tiles layout_tiles.addWidget(self.btn_tile0) layout_tiles.addWidget(self.btn_tile1) layout_tiles.addWidget(self.btn_tile2) layout_tiles.addWidget(self.btn_tile3) layout_tiles.addWidget(self.btn_tile4) layout_tiles.addWidget(self.btn_tile5) layout_tiles.addWidget(self.btn_tile6) # best moves layout_moves.addWidget(self.btn_move0, 0, 0) layout_moves.addWidget(self.btn_move1, 0, 1) layout_moves.addWidget(self.btn_move2, 0, 2) layout_moves.addWidget(self.btn_move3, 0, 3) layout_moves.addWidget(self.btn_move4, 0, 4) layout_moves.addWidget(self.btn_move5, 1, 0) layout_moves.addWidget(self.btn_move6, 1, 1) layout_moves.addWidget(self.btn_move7, 1, 2) layout_moves.addWidget(self.btn_move8, 1, 3) layout_moves.addWidget(self.btn_move9, 1, 4) # left layout_left.addWidget(lbl_tile_bag) layout_left.addWidget(self.lne_tile_bag) layout_left.addWidget(lbl_draw_stats) layout_left.addWidget(self.draw_stats_canvas) # center layout_center.addWidget(self.board_canvas) layout_center.addWidget(self.grp_tiles) # right layout_right.addWidget(lbl_strength) layout_right.addWidget(self.strength_canvas) layout_right.addWidget(lbl_moves) layout_right.addWidget(self.grp_moves) layout_right.addWidget(self.btn_play) # main layout_main.addWidget(self.grp_left) layout_main.addWidget(self.grp_center) layout_main.addWidget(self.grp_right) # main GUI layout_gui.addWidget(lbl_title) layout_gui.addWidget(self.grp_main) #%% File Menu # actions - new game act_new_game = QAction('New Game', self) act_new_game.setShortcut('Ctrl+N') act_new_game.setStatusTip('Starts a new game.') act_new_game.triggered.connect(self.act_new_game_func) # actions - options act_options = QAction('Options', self) act_options.setShortcut('Ctrl+O') act_options.setStatusTip('Opens the advanced option settings.') act_options.triggered.connect(self.act_options_func) # actions - quit game act_quit = QAction('Exit', self) act_quit.setShortcut('Ctrl+Q') act_quit.setStatusTip('Exits the application.') act_quit.triggered.connect(self.close) # menubar self.statusBar() menu_bar = self.menuBar() file_menu = menu_bar.addMenu('&File') file_menu.addAction(act_new_game) file_menu.addAction(act_options) file_menu.addAction(act_quit) #%% Finalization # Call wrapper to initialize GUI self.wrapper() # GUI final layout properties self.center() self.setWindowTitle('Cheater GUI') self.setWindowIcon(QtGui.QIcon(os.path.join(get_root_dir(), 'scrabble.png'))) self.show()