def paint_color_window(x_win, y_win, width, height, focus): global data_color global buff global color_action global message_listering click, x_click, y_click = buff for i in range(len(color)): for j in range(len(color[0])): width_rect = width / len(color[0]) height_rect = height / len(color) canvas.fill_style(color[i][j]) canvas.fill_rect(x_win + width_rect * j, y_win + height_rect * i, width_rect, height_rect) data_color.append([ x_win + width_rect * j, y_win + height_rect * i, width_rect, height_rect, color[i][j] ]) if click and focus: for el in data_color: x, y, wid, hei, col = el if x < x_click < x + wid and y < y_click < y + hei: color_action = col print(col) buff[0] = False message_listering.append(['window', 'Color', 'close']) message_listering.append(['window', 'Paint', 'enable_focus']) break
def create_object(): canvas.fill_style('LightskyBlue') canvas.fill_rect(0, 0, 349, 349) canvas.fill_style('CornflowerBlue') canvas.fill_text('Windows Py', 175, 50, font='Tahoma', size=42, align='center') global stack el_focus = -1 if data_property: for index, el in enumerate(data_property, 0): if el[0] == 'window' and el[7]: el_focus = index if el[0] == 'button' and el[1]: obj, show, x, y, width, height, caption, press = el button(x, y, width, height, caption, press) elif el[0] == 'window' and el[1]: obj, show, x, y, width, height, caption, focus = el window(x, y, width, height, caption, focus, index) elif el[0] == 'taskbar' and el[1]: obj, show, height = el taskbar(height) elif el[0] == 'start_menu' and el[1]: obj, show, x, y, width, height, non, focus = el start_menu(x, y, width, height, focus) if el_focus >= 0 and data_property[el_focus][1]: obj, show, x, y, width, height, caption, focus = data_property[ el_focus] window(x, y, width, height, caption, focus, el_focus)
def button(caption, x, y, width, height): canvas.fill_style('Black') canvas.fill_rect(x, y, width + 2, height + 2) # Тень canvas.fill_style('PaleTurquoise') canvas.fill_rect(x, y, width, height) canvas.fill_style('Black') canvas.fill_text(caption, x + width / 2 + 1, y + width // 7 + 1, 'Tahoma', width // 9, 'center') # Тень canvas.fill_style('DarkGreen') canvas.fill_text(caption, x + width / 2, y + width // 7, 'Tahoma', width // 9, 'center')
def draw_menu(x, y, width): canvas.clear() global MENU global initMenu height = width if initMenu: canvas.fill_style('Black') canvas.fill_rect(x, y, width + 2, height + 2) canvas.fill_style('Grey') canvas.fill_rect(x, y, width, height) for el in MENU: button(el[0], el[1], el[2], el[3], el[4]) else: initMenu = True count = len(MENU) for i in range(count): MENU[i][1] = x + (width - width * 0.8) / 2 MENU[i][2] = y + (i + 1) * height / (count + 1) - width * 0.8 * 0.2 / 2 MENU[i][3] = width * 0.8 MENU[i][4] = width * 0.8 * 0.2
def taskbar(height): global buff global init_task global message_listering click, x_click, y_click = buff border_col, background_content, text_col, header_col = theme[theme_active] x, y, width = 0, 349 - height, 349 for el in data_property: if el[0] == 'button' and el[6] == 'Start': if not init_task: el[2], el[3], el[4], el[5] = x + 2, y + 2, 50, height - 4 init_task = True else: if click: if el[2] < x_click < el[2] + el[4] and el[ 3] < y_click < el[3] + el[5]: message_listering.append( ['start_menu', '', 'enable_focus']) buff[0] = False canvas.fill_style('SlateGray') canvas.fill_rect(x, y - 2, width, height) canvas.fill_style(border_col) canvas.fill_rect(x, y, width, height) time_ = dt.datetime.now() time_str = time_.strftime("%H:%M") day_str = time_.strftime("%d") canvas.fill_style('Black') canvas.fill_text(time_str, x + 320, y + 20, 'Tahoma', 14, 'center')
def calc(x, y, width, height, focus): global calc_butt global init_calc global buff global lcd_value global memory border_col, background_content, text_col, header_col = theme[theme_active] canvas.set_color(border_col) canvas.stroke_rect(x + 5, y + 5, width - 10, 30) canvas.fill_style('Black') canvas.fill_text(lcd_value, x + width - 10, y + 5 + 30 - 7, 'Tahoma', 22, 'right') if memory[0] != 0: canvas.fill_style('Black') canvas.fill_text('M', x + 8, y + 18, 'Tahoma', 14, 'left') x_but, y_but, width_but, height_but = x + 5, y + 40, (width - 10) / len( calc_butt[0]) - 4, (height - 45) / len(calc_butt) - 4 border = 5 click, x_click, y_click = buff if not init_calc: for i in range(len(calc_butt)): for j in range(len(calc_butt[0])): x_b = x_but + (width_but + border) * j y_b = y_but + (height_but + border) * i button(x_b, y_b, width_but, height_but, calc_butt[i][j][0], False) calc_butt[i][j][1] = x_b calc_butt[i][j][2] = y_b init_calc = True else: for i in range(len(calc_butt)): for j in range(len(calc_butt[0])): cap, x_but, y_but = calc_butt[i][j] button(x_but, y_but, width_but, height_but, cap, False) if click and focus: if x_but < x_click < x_but + width_but and y_but < y_click < y_but + height_but: calc_handler(cap) buff[0] = False
def start_menu(x, y, width, height, focus): global buff global menu_icon global init_menu_icon global message_listering border_col, background_content, text_col, header_col = theme[theme_active] for el in data_property: if el[0] == 'start_menu' and el[1]: pass canvas.fill_style('Black') canvas.fill_rect(x, y, width + 1, height) canvas.fill_style(border_col) canvas.fill_rect(x, y, width, height) if not init_menu_icon: for index, el in enumerate(menu_icon, 0): el[1], el[2], el[3], el[ 4] = x + 2, y + 2 + 27 * index, width - 4, 25 init_menu_icon = True else: click, x_click, y_click = buff for index, el in enumerate(menu_icon, 0): text, x_icon, y_icon, width_icon, height_icon = el canvas.fill_style('Silver') canvas.fill_rect(x_icon, y_icon, width_icon, height_icon) canvas.fill_style('Black') canvas.fill_text(text, x + 2 + width_icon / 2, y_icon + 17, 'Tahoma', 14, 'center') if click: if x_icon < x_click < x_icon + width_icon and y_icon < y_click < y_icon + height_icon: if text == 'Paint': message_listering.append( ['window', 'Paint', 'enable_focus']) message_listering.append(['start_menu', '', 'close']) if text == 'Calculator': message_listering.append( ['window', 'Calc', 'enable_focus']) message_listering.append(['start_menu', '', 'close']) if text == 'XXX_Por': message_listering.append( ['XXX', 'XXX', 'enable_focus']) message_listering.append(['start_menu', '', 'close']) else: message_listering.append(['start_menu', '', 'close'])
def button(x, y, width=80, height=25, caption='OK', press=False): if not press: # рисование тени canvas.fill_style('Black') canvas.fill_rect(x + 1, y + 1, width, height) canvas.fill_style(theme[theme_active][0]) canvas.fill_rect(x, y, width, height) canvas.fill_style('Black') canvas.fill_text(caption, x + width / 2, y + height * 0.7, font='Tahoma', size=12, align='center')
def message(caption, x, y, width): canvas.clear() height = width * 0.2 canvas.fill_style('Black') canvas.fill_rect(x, y - height / 2, width + 2, height + 2) canvas.fill_style('Grey') canvas.fill_rect(x, y - height / 2, width, height) if caption == 'YOU WIN': color1 = 'DarkBlue' color2 = 'DeepSkyBlue' elif caption == 'YOU LOST': color1 = 'DarkRed' color2 = 'Red' else: color1 = 'Black' color2 = 'Green' canvas.fill_style(color1) canvas.fill_text(caption, x + width // 2 + 2, y + height * 0.3 + 2, 'Tahoma', width // 6, 'center') canvas.fill_style(color2) canvas.fill_text(caption, x + width // 2, y + height * 0.3, 'Tahoma', width // 6, 'center')
def paint_draw(): global paint_obj for el in paint_obj: if el[0] == 'point': canvas.fill_style(el[3]) canvas.fill_circle(el[1], el[2], 2) if el[0] == 'line': canvas.set_color(el[5]) canvas.move_to(el[1], el[2]) canvas.line_to(el[3], el[4]) if el[0] == 'rect': if el[6]: canvas.fill_style(el[5]) canvas.fill_rect(el[1], el[2], el[3], el[4]) else: canvas.set_color(el[5]) canvas.stroke_rect(el[1], el[2], el[3], el[4]) if el[0] == 'circle': if el[5]: canvas.fill_style(el[4]) canvas.fill_circle(el[1], el[2], el[3]) else: canvas.set_color(el[4]) canvas.circle(el[1], el[2], el[3])
def window(x, y, width, height, caption, focus, target): global data_property global buff global stack global message_listering border_col, background_content, text_col, header_col = theme[theme_active] border = 3 height_caption = 20 margin_text = 5 canvas.fill_style('Black') canvas.fill_rect(x + 1, y + 1, width, height) # Тень окна canvas.fill_style(border_col) canvas.fill_rect(x, y, width, height) # Основное окно if focus: canvas.fill_style(header_col) else: canvas.fill_style('Grey') canvas.fill_rect(x + border, y + border, width - border * 2, height_caption) # Окно шапки canvas.fill_style('Black') canvas.fill_text(caption, x + border + margin_text + 1, y + border + 15 + 1, 'Tahoma', 14, 'left') # тень текста шапки canvas.fill_style(text_col) canvas.fill_text(caption, x + border + margin_text, y + border + 15, 'Tahoma', 14, 'left') # текст шапки canvas.fill_style(background_content) canvas.fill_rect(x + border, y + border * 2 + height_caption, width - border * 2, height - border * 3 - height_caption) # фон окна, контента x_but_close = x + width - margin_text - 14 + 1 y_but_close = y + 6 canvas.fill_style('Black') canvas.fill_rect(x_but_close + 1, y_but_close + 1, 14, 14) canvas.fill_style('DarkGray') canvas.fill_rect(x_but_close, y_but_close, 14, 14) canvas.set_color('Black') canvas.line_width(2) canvas.radius_line(x_but_close + 2, y_but_close + 2, 135, 13) canvas.radius_line(x_but_close + 2 + 9, y_but_close + 2, 225, 13) # Реализацию вызовов нужных функций можно переделать с использованием передачи параметра-функции, возможно доработаю в следующих версиях if data_property[target][6] == 'Paint': paint(x + border, y + border * 2 + height_caption, width - border * 2, height - border * 3 - height_caption, focus) if data_property[target][6] == 'Color': paint_color_window(x + border, y + border * 2 + height_caption, width - border * 2, height - border * 3 - height_caption, focus) if data_property[target][6] == 'Calc': calc(x + border, y + border * 2 + height_caption, width - border * 2, height - border * 3 - height_caption, focus) # Обработчик закрытия окна click, x_click, y_click = buff if click: if x_but_close + 14 > x_click > x_but_close and y_but_close + 14 > y_click > y_but_close: message_listering.append(['window', caption, 'close']) buff[0] = False if click and not focus: if x < x_click < x + width and y < y_click < y + height_caption: message_listering.append(['window', caption, 'enable_focus']) buff[0] = False
def paint(x, y, width, height, focus): global init global data_property global buff # Буфер (список) для передачи состояния мыши и координат global but # Список кнопок панели управления global but_ global fill_on canvas.fill_style(theme[theme_active][0]) height_panel = 55 canvas.fill_rect(x, y, width, height_panel) canvas.fill_style('Black') canvas.fill_text('Панель инструментов', x + width / 2, y + height / 12 / 2, 'Tahoma', 12, 'center') if not init: # инициализация, выполняется один раз. x_but = x y_but = y + height / 6 - height / 10 x_but_ = x y_but_ = y_but + height / 12 - 1 + 2 count_button_ = 3 count_button = 5 button_text = ['Point', 'Line', 'Rect', 'Circle', 'Color'] button_text_ = ['New', 'Fill OFF', 'Undo'] for i in range(5): but.append([ x_but + i * width / count_button, y_but, width / count_button, height / 12 - 1, button_text[i], False ]) button(but[i][0], but[i][1], but[i][2], but[i][3], but[i][4], but[i][5]) if i < count_button_: but_.append([ x_but_ + i * width / count_button_, y_but_, width / count_button_, height / 12 - 1, button_text_[i], False ]) button(but_[i][0], but_[i][1], but_[i][2], but_[i][3], but_[i][4], but_[i][5]) init = True else: # Если инициализация была выполнена, выполняется код ниже for index, el in enumerate(but, 0): x_butt, y_butt, width_butt, height_butt, caption_butt, press = el click, x_click, y_click = buff # Здесь идет расчет, чтобы была нажата только одна из кнопок верхнего ряда if click and x_butt + width_butt > x_click > x_butt and y_butt + height_butt > y_click > y_butt and focus: but[index][5] = True buff[0] = False for j in range(5): if j != index: but[j][5] = False click, x_click, y_click = buff for i in range(5): button(but[i][0], but[i][1], but[i][2], but[i][3], but[i][4], but[i][5]) if i < len(but_): x_but, y_but, width_but, height_but, caption_but, press = but_[i] button(x_but, y_but, width_but, height_but, caption_but, press) if click: if x_but < x_click < x_but + width_but and y_but < y_click < y_but + height_but and focus: if caption_but == 'New': paint_obj.clear() elif caption_but == 'Undo': if paint_obj: paint_obj.pop() elif caption_but == 'Fill OFF': but_[i][4] = 'Fill ON' fill_on = True elif caption_but == 'Fill ON': but_[i][4] = 'Fill OFF' fill_on = False buff[0] = False paint_canvas(x, y + height_panel, width, height - height_panel, focus) paint_draw()