From 66beba6b0d1b29cfc137edb40bd1af6fde651924 Mon Sep 17 00:00:00 2001 From: Ivan Date: Thu, 22 Apr 2021 20:35:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D1=84=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D0=B9=20=D0=B8=D0=BD?= =?UTF-8?q?=D1=82=D0=B5=D1=80=D1=84=D0=B5=D0=B9=D1=81=20=D1=81=20=D1=87?= =?UTF-8?q?=D0=B0=D1=81=D1=82=D1=8C=D1=8E=20=D1=84=D1=83=D0=BD=D0=BA=D1=86?= =?UTF-8?q?=D0=B8=D0=B9.=20=D0=94=D0=BB=D1=8F=20=D0=B7=D0=B0=D0=BF=D1=83?= =?UTF-8?q?=D1=81=D0=BA=D0=B0=20=D0=BD=D0=B5=D0=BE=D0=B1=D1=85=D0=BE=D0=B4?= =?UTF-8?q?=D0=B8=D0=BC=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F=20=D1=81=D1=80=D0=B5=D0=B4=D1=8B=20DEBUG,=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BA=D0=B0=20=D1=87=D1=82=D0=BE=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D0=BC=20=D0=BD=D0=B0=20=D0=B8?= =?UTF-8?q?=D0=B3=D1=80=D0=BE=D0=B2=D1=8B=D1=85=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D1=85=20-=20=D0=B1=D0=B5=D0=B7=20=D0=B1=D1=8D=D0=BA?= =?UTF-8?q?=D1=8D=D0=BD=D0=B4=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/login.py | 68 +++++++++++++++++++++++ frontend/message.py | 20 +++++++ frontend/todo_tk.py | 51 +++++++++++++++-- frontend/user.py | 13 +++++ frontend/workspace.py | 124 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 271 insertions(+), 5 deletions(-) create mode 100644 frontend/login.py create mode 100644 frontend/message.py create mode 100644 frontend/workspace.py diff --git a/frontend/login.py b/frontend/login.py new file mode 100644 index 0000000..64ba0e3 --- /dev/null +++ b/frontend/login.py @@ -0,0 +1,68 @@ +import tkinter as tk +from user import User +import message + + +class LoginFrame(tk.Frame): + + loggedIn = False + + def __init__(self, master=None, url=None) -> None: + """ + Функция инициаизации класса + """ + super().__init__(master) + + # Иницализируем параметры окна + self.master = master + self.pack(fill=tk.BOTH, expand=1) + + # self.grid(row=0, column=0, sticky=tk.N + tk.S + tk.E + tk.W) + # tk.Grid.rowconfigure(master, 0, weight=1) + # tk.Grid.columnconfigure(master, 0, weight=1) + + # Иницализируем параметры пользователя + self.user = User(url=url) + + # Настраиваем размеры и включаем иницализацию + self.initAUTH() + + def login_clicked(self) -> None: + """ + Функция авторизации + """ + try: + self.user.auth(self.login.get(), self.password.get()) + self.loggedIn = True + except Exception as ex: + print(ex) + message.invalid_login() + + def initAUTH(self) -> None: + """ + Создает окно авторизации программы + """ + # Конфигурируем сетку + for rows in range(25): + tk.Grid.rowconfigure(self, rows, weight=1) + + for columns in range(25): + tk.Grid.columnconfigure(self, columns, weight=1) + + # Подпись и поле ввода для логина + login_label = tk.Label(self, text="Введите логин") + login_label.grid(row=9, column=12, columnspan=3, rowspan=1, sticky="nsew") + + self.login = tk.Entry(self) + self.login.grid(row=10, column=12, columnspan=3, rowspan=1, sticky="nsew") + + # Подпись и поле ввода для пароля + password_label = tk.Label(self, text="Введите пароль") + password_label.grid(row=11, column=12, columnspan=3, rowspan=1, sticky="nsew") + + self.password = tk.Entry(self, show="*") + self.password.grid(row=12, column=12, columnspan=3, rowspan=1, sticky="nsew") + + # Кнопка авториазции + btn = tk.Button(self, text="Войти", command=self.login_clicked) + btn.grid(row=14, column=12, columnspan=3, rowspan=1, sticky="nsew") diff --git a/frontend/message.py b/frontend/message.py new file mode 100644 index 0000000..824d07b --- /dev/null +++ b/frontend/message.py @@ -0,0 +1,20 @@ +from tkinter import messagebox as mb + +TITLE_INFO_BOX = "Сообщение!" +MESSAGE_INVALID_LOGIN = "Неправильный логин или пароль" +MESSAGE_EMPTY = "Сдесь могло быть ваше сообщение" + + +def infobox(msg: str = None) -> None: + """ + Показывает передаваемое сообщение в messagebox + + :param msg: передаваемое сообщение + """ + if msg is None: + msg = MESSAGE_EMPTY + mb.showinfo(TITLE_INFO_BOX, msg) + + +def invalid_login(): + infobox(MESSAGE_INVALID_LOGIN) diff --git a/frontend/todo_tk.py b/frontend/todo_tk.py index 41e0e99..831ac6c 100644 --- a/frontend/todo_tk.py +++ b/frontend/todo_tk.py @@ -1,14 +1,55 @@ #!/usr/bin/env python3 +import sys import tkinter as tk +from login import LoginFrame +from workspace import WorkSpaceFrame + +if "win" in sys.platform.lower(): + DEFAULT_URL = "http://localhost:8000" +else: + DEFAULT_URL = "http://0.0.0.0:8000" + +BASE_W = 580 +BASE_H = 400 + +TITLE_APP = "ToDo Application" -class Application(tk.Frame): - def __init__(self, master=None): - super().__init__(master) +class Application(tk.Tk): + def __init__(self): + super().__init__() + self.center_window() + self.title(TITLE_APP) + + def login(self): + """Возвращает пользователя - его можно потом сериализовать""" + self.frame = LoginFrame(master=self, url=DEFAULT_URL) + while not self.frame.loggedIn: + self.update_idletasks() + self.update() + self.frame.destroy() + return self.frame.user + + def main(self, user): + self.frame = WorkSpaceFrame(master=self, user=user) + self.mainloop() + + def center_window(self, width: str = BASE_W, heigh: str = BASE_H) -> None: + """ + Центрирует приложение по центру экрана + + :param width: ширина окна + :param heigh: высота окна + """ + sw = self.winfo_screenwidth() + sh = self.winfo_screenheight() + + x = (sw - width) / 2 + y = (sh - heigh) / 2 + self.geometry("%dx%d+%d+%d" % (width, heigh, x, y)) if __name__ == "__main__": app = Application() - app.master.title("ToDo") - app.mainloop() + app.main(app.login()) diff --git a/frontend/user.py b/frontend/user.py index 38cda9b..5cdff8e 100644 --- a/frontend/user.py +++ b/frontend/user.py @@ -1,3 +1,5 @@ +import os + from datetime import datetime from api import UserApi @@ -9,6 +11,10 @@ class ToDoList(object): self.items = items self.created_at = created_at + def __iter__(self): + for item in self.items: + yield item + def __getitem__(self, index): return self.items[index] @@ -18,6 +24,9 @@ class ToDoList(object): def __str__(self): return f"[{self.id}] {self.title}" + def index(self, value): + return self.items.index(value) + # ToDo def remove(self, index): self.items.remove(self.items[index]) @@ -64,6 +73,10 @@ class ToDoItem(object): class User(UserApi): + def auth(self, user, passwd): + if "DEBUG" in os.environ: + return + UserApi.auth(self, user, passwd) # ToDo items = [ diff --git a/frontend/workspace.py b/frontend/workspace.py new file mode 100644 index 0000000..9a33bd1 --- /dev/null +++ b/frontend/workspace.py @@ -0,0 +1,124 @@ +import tkinter as tk + + +def str_time(time): + return time.strftime("%Y-%m-%d %H:%M:%S") + + +class ToDoItemWidget(tk.Frame): + def __init__(self, *args, item, parent=None, **argv): + super().__init__(*args, **argv) + + self.parent = parent + self.item = item + + self.noteLabel = tk.Label(self, text=item.text, width=15) + self.noteLabel.pack(side="left") + + self.finished = tk.IntVar(value=int(item.finished)) + self.finishedButton = tk.Checkbutton( + self, variable=self.finished, command=self.finishedButton_command + ) + self.finishedButton.pack(side="left") + + self.createdAt = tk.Label(self, text=str_time(item.created_at), width=15) + self.createdAt.pack(side="left") + + self.remove = tk.Button(self, text="Удалить", command=lambda: parent.remove(self.item)) + self.remove.pack(side="left") + + def finishedButton_command(self): + self.item.modify(finished=self.finished.get() > 0) + + +class ToDoListWidget(tk.Frame): + def __init__(self, *args, **argv): + super().__init__(*args, **argv) + + def fill(self, itemList): + + self.header = tk.Label(self, text="Текст | Выполнено | Время создания") + self.header.pack(side="top", fill="y") + + self.itemList = itemList + + for item in itemList: + item = ToDoItemWidget(self, item=item, parent=self) + item.pack(side="top", fill="y") + + self.itemToAdd = tk.Text(self, width=15, height=1) + self.itemToAdd.pack(side="top") + + add = tk.Button(self, text="Добавить", command=self.add_command) + add.pack(side="top") + + def update(self, itemList=None): + self.clear() + if itemList is None: + self.fill(self.itemList) + else: + self.fill(itemList) + + def add_command(self): + self.itemList.append(self.itemToAdd.get(1.0, "end")) + self.update() + + def remove(self, item): + self.itemList.remove(self.itemList.index(item)) + self.update() + + def clear(self): + for widget in self.winfo_children(): + widget.destroy() + + +class WorkSpaceFrame(tk.Frame): + def __init__(self, user, master=None, url=None) -> None: + """ + Функция инициаизации класса + """ + super().__init__(master) + + self.master = master + self.user = user + + self.pack(fill=tk.BOTH, expand=1) + self.initLayout(user) + + def initLayout(self, user): + + # data + self.lists = user.fetchUserLists() + + # select list box + self.listBox = tk.Listbox(self, width=30, selectmode=tk.SINGLE) + self.listBox.pack(side="left", fill="y") + self.listBox.bind("<>", self.listBox_selected) + + # scroll bar + scrollbar = tk.Scrollbar(self, orient="vertical") + scrollbar.config(command=self.listBox.yview) + scrollbar.pack(side="left", fill="y") + + # add scroll bar to list box + self.listBox.config(yscrollcommand=scrollbar.set) + + # fill list box + for item in self.lists: + s = f"{str(item)}: {item.created_at.strftime('%Y-%m-%d %H:%M:%S')}" + self.listBox.insert(tk.END, s) + self.listBox.pack() + len(self.lists) > 0 and self.listBox.selection_set(first=0) + + # todo lists + self.toToList = ToDoListWidget(self) + self.toToList.pack(side="left", fill="both", expand=1) + + def listBox_selected(self, *args): + + self.toToList.clear() + + selection = self.listBox.curselection() + cur = selection[0] + + self.toToList.fill(self.lists[cur])