/
ParaD
/
Server
Обзор
Документация
Войти
/
ParaD
/
Server
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
frontend_data.py
176 строк
9 KB
Rayfaga
first_commit
01 мар 2025, 20:02
01 мар 2025, 20:02
c75de2b
Код
Авторство
О чём код?
import pygame import sys import subprocess from tkinter import Tk, filedialog import shutil import os from talk.send_csv import send_csv python_executable = sys.executable def read_robot_count(file_path): try: with open(file_path, "r") as f: return int(f.read().strip()) except (FileNotFoundError, ValueError): print(f"Ошибка: файл {file_path} не найден или содержит некорректные данные!") return 0 def draw_text(surface, text, font, color, x, y): text_surface = font.render(text, True, color) text_rect = text_surface.get_rect() text_rect.topleft = (x, y) surface.blit(text_surface, text_rect) def draw_button(surface, text, font, color, x, y, width, height): pygame.draw.rect(surface, (0, 0, 0), (x, y, width, height), 2) draw_text(surface, text, font, color, x + 10, y + 10) message_text = "" message_color = (0, 0, 0) message_time = 0 def recieve_info(): global message_text, message_color, message_time root = Tk() root.withdraw() file_path = filedialog.askopenfilename(title="Выберите .csv файл для получения информации", filetypes=[(".csv файл", "*.csv")]) if file_path: try: shutil.copy(file_path, "./info_pass.csv") message_text = "Успешно! Данные считаны!" message_color = (56, 87, 35) except Exception as e: message_text = f"Ошибка: {e}" message_color = (236, 28, 36) else: message_text = "Файл не выбран!" message_color = (236, 28, 36) message_time = pygame.time.get_ticks() root.destroy() def send_info(): global message_text, message_color, message_time root = Tk() root.withdraw() folder_path = filedialog.askdirectory(title="Выберите место для установки") if folder_path: try: shutil.copy("./bdpokyp.csv", folder_path) message_text = "Успешно! Данные считаны!" message_color = (56, 87, 35) except Exception as e: message_text = f"Ошибка: {e}" message_color = (236, 28, 36) else: message_text = "Файл не выбран!" message_color = (236, 28, 36) if folder_path: message_text = f"Успешно! Данные перенесены!" message_color = (56, 87, 35) else: message_text = "Папка не выбрана!" message_color = (236, 28, 36) message_time = pygame.time.get_ticks() root.destroy() def send_all_robots(): global message_text, message_color, message_time robot_count_file = "./variable_txt/count_robots.txt" for i in range(1, read_robot_count(robot_count_file) + 1): if os.path.isfile('./info_pass.csv'): vrem = send_csv('./info_pass.csv', i) if vrem == "NO_CONNECT": message_text = f'Нет соединения с роботом {i}' message_color = (236, 28, 36) else: message_text = f'Отправлено!' message_color = (236, 28, 36) else: message_text = 'Отсутствует файл info_pass.csv' message_color = (236, 28, 36) message_time = pygame.time.get_ticks() def open_database(): global message_text, message_color, message_time try: if os.path.exists("bdpokyp.csv"): os.startfile("bdpokyp.csv") else: message_text = "Ошибка! База данных не обнаружена!" message_color = (236, 28, 36) except Exception as e: message_text = "Ошибка "+ str(e) message_color = (236, 28, 36) message_time = pygame.time.get_ticks() def run(screen): global message_text, message_color, message_time screen_info = pygame.display.Info() global screen_width global screen_height screen_width, screen_height = screen_info.current_w, screen_info.current_h pygame.display.set_caption("Российские Железные Дороги - данные") background_color = (220, 220, 220) line_color = (38, 38, 38) line_thickness = 5 font = pygame.font.SysFont("Arial", 32, bold=True) button_font = pygame.font.SysFont("Arial", 20, bold=True) button_width = screen_width // 3 button_height = 40 buttons = [ {"text": "Получить информацию с USB-накопителя", "x": screen_width // 48, "y": screen_height // 5, "width": button_width, "height": button_height, "action": recieve_info}, {"text": "Перенести информацию на всех роботов", "x": screen_width // 48, "y": screen_height // 5 + button_height + screen_height // 36, "width": button_width, "height": button_height, "action": send_all_robots}, {"text": "Перенести данные на USB-накопитель", "x": screen_width // 2 + screen_width // 48, "y": screen_height // 5, "width": button_width, "height": button_height, "action": send_info}, {"text": "Показать базу данных", "x": screen_width // 2 + screen_width // 48, "y": screen_height // 5 + button_height + screen_height // 36, "width": button_width, "height": button_height, "action": open_database} ] running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: mouse_x, mouse_y = event.pos if button_exit_x <= mouse_x <= button_exit_x + button_exit_width and button_exit_y <= mouse_y <= button_exit_y + button_exit_height: return 0 for button in buttons: if button["x"] <= mouse_x <= button["x"] + button["width"] and button["y"] <= mouse_y <= button["y"] + button["height"]: button["action"]() screen.fill(background_color) left_rect_width = screen_width // 2 left_rect_height = screen_height pygame.draw.rect(screen, (220, 220, 220), (0, 0, left_rect_width, left_rect_height)) draw_text(screen, "Информация о пассажирах на маршруте", font, (0, 0, 0), screen_width // 48, screen_height // 36) for i, button in enumerate(buttons[:2]): draw_button(screen, button["text"], button_font, (0, 0, 0), button["x"], button["y"], button["width"], button["height"]) right_rect_x = screen_width // 2 pygame.draw.rect(screen, (220, 220, 220), (right_rect_x, 0, left_rect_width, left_rect_height)) draw_text(screen, "База данных постоянных покупателей", font, (0, 0, 0), right_rect_x + screen_width // 48, screen_height // 36) for _, button in enumerate(buttons[2:]): draw_button(screen, button["text"], button_font, (0, 0, 0), button["x"], button["y"], button["width"], button["height"]) pygame.draw.line(screen, line_color, (screen_width // 2, 0), (screen_width // 2, screen_height), line_thickness) pygame.draw.rect(screen, '#f97f6e', (0, screen_height-screen_height // 6, screen_width, screen_height // 6)) if message_text: draw_text( screen, message_text, pygame.font.SysFont("Arial", 60, bold=True), (38, 38, 38), screen_width // 26, screen_height - screen_height // 8, ) if pygame.time.get_ticks() - message_time > 3000: message_text = "" button_exit_text_color = (38, 38, 38) button_exit_color = (220, 220, 220) button_exit_width = screen_width//6 button_exit_height = 4*screen_height//36 button_exit_x = screen_width - button_exit_width - screen_width//12 button_exit_y = screen_height - button_exit_height - screen_height//36 button_exit_font = pygame.font.SysFont("Arial", 26, bold=True) pygame.draw.rect(screen, button_exit_color, (button_exit_x, button_exit_y, button_exit_width, button_exit_height)) button_exit_text_surface = button_exit_font.render("Вернуться назад", True, button_exit_text_color) button_exit_text_rect = button_exit_text_surface.get_rect(center=(button_exit_x + button_exit_width // 2, button_exit_y + button_exit_height // 2)) screen.blit(button_exit_text_surface, button_exit_text_rect) pygame.display.flip() if __name__=="__main__": pygame.init() run(pygame.display.set_mode((pygame.display.Info().current_w, pygame.display.Info().current_h)))