Загрузка данных
```python
import sys
from PySide6.QtWidgets import (
QApplication, QMainWindow, QWidget, QDialog,
QLabel, QLineEdit, QPushButton, QVBoxLayout,
QHBoxLayout, QScrollArea, QFrame, QMessageBox
)
from PySide6.QtCore import Qt
USERS = [
{"login": "admin", "password": "1234", "full_name": "Администратор"}
]
PRODUCTS = [
{
"category": "Напитки",
"name": "Вода",
"description": "Питьевая вода",
"manufacturer": "Аква",
"supplier": "Поставщик 1",
"price": 50.00,
"unit": "шт.",
"quantity": 20,
"discount": 5
},
{
"category": "Продукты",
"name": "Шоколад",
"description": "Молочный шоколад",
"manufacturer": "Сладость",
"supplier": "Поставщик 2",
"price": 120.00,
"unit": "шт.",
"quantity": 0,
"discount": 20
},
{
"category": "Бытовая химия",
"name": "Мыло",
"description": "Мыло для рук",
"manufacturer": "Чистота",
"supplier": "Поставщик 3",
"price": 80.00,
"unit": "шт.",
"quantity": 15,
"discount": 18
}
]
class LoginWindow(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Авторизация")
self.setFixedSize(340, 280)
self.user = None
layout = QVBoxLayout(self)
layout.setSpacing(10)
title = QLabel("Вход в систему")
title.setAlignment(Qt.AlignmentFlag.AlignCenter)
title.setStyleSheet("font-size: 20px; font-weight: bold;")
layout.addWidget(title)
layout.addWidget(QLabel("Логин"))
self.login_entry = QLineEdit()
self.login_entry.setPlaceholderText("Введите логин")
layout.addWidget(self.login_entry)
layout.addWidget(QLabel("Пароль"))
self.password_entry = QLineEdit()
self.password_entry.setPlaceholderText("Введите пароль")
self.password_entry.setEchoMode(QLineEdit.EchoMode.Password)
layout.addWidget(self.password_entry)
login_button = QPushButton("Войти")
login_button.clicked.connect(self.login)
layout.addWidget(login_button)
guest_button = QPushButton("Войти как гость")
guest_button.clicked.connect(self.login_as_guest)
layout.addWidget(guest_button)
self.password_entry.returnPressed.connect(self.login)
def login(self):
login = self.login_entry.text().strip()
password = self.password_entry.text()
for user in USERS:
if user["login"] == login and user["password"] == password:
self.user = user["full_name"]
self.accept()
return
QMessageBox.warning(
self, "Ошибка входа", "Неверный логин или пароль"
)
def login_as_guest(self):
self.user = "Гость"
self.accept()
def get_background(product):
if product["quantity"] == 0:
return "#ADD8E6"
if product["discount"] > 17:
return "#FFDEAD"
return "#FFFFFF"
class ProductsWindow(QMainWindow):
def __init__(self, username):
super().__init__()
self.username = username
self.setWindowTitle("Список товаров")
self.resize(850, 600)
central = QWidget()
self.setCentralWidget(central)
main_layout = QVBoxLayout(central)
header = QHBoxLayout()
title = QLabel("Список товаров")
title.setStyleSheet("font-size: 22px; font-weight: bold;")
header.addWidget(title)
header.addStretch()
user_label = QLabel(username)
header.addWidget(user_label)
logout_button = QPushButton("Выход")
logout_button.clicked.connect(self.logout)
header.addWidget(logout_button)
main_layout.addLayout(header)
self.scroll = QScrollArea()
self.scroll.setWidgetResizable(True)
self.list_widget = QWidget()
self.list_layout = QVBoxLayout(self.list_widget)
self.list_layout.setAlignment(Qt.AlignmentFlag.AlignTop)
self.scroll.setWidget(self.list_widget)
main_layout.addWidget(self.scroll)
self.show_products()
def show_products(self):
for product in PRODUCTS:
background = get_background(product)
card = QFrame()
card.setFrameShape(QFrame.Shape.Box)
card.setStyleSheet(
f"QFrame {{ background-color: {background}; "
"border: 1px solid #555; }}"
)
card_layout = QHBoxLayout(card)
info = QWidget()
info_layout = QVBoxLayout(info)
info_layout.setContentsMargins(5, 5, 5, 5)
lines = [
f'{product["category"]} | {product["name"]}',
f'Описание: {product["description"]}',
f'Производитель: {product["manufacturer"]}',
f'Поставщик: {product["supplier"]}',
f'Цена: {product["price"]:.2f}',
f'Единица измерения: {product["unit"]}',
f'Количество на складе: {product["quantity"]}'
]
for index, text in enumerate(lines):
label = QLabel(text)
label.setWordWrap(True)
if index == 0:
label.setStyleSheet(
"font-size: 14px; font-weight: bold; "
"border: none;"
)
else:
label.setStyleSheet("border: none;")
info_layout.addWidget(label)
card_layout.addWidget(info, 1)
discount = QLabel(f'{product["discount"]}%')
discount.setAlignment(Qt.AlignmentFlag.AlignCenter)
discount.setMinimumWidth(90)
discount.setStyleSheet(
"font-size: 22px; font-weight: bold; border: none;"
)
card_layout.addWidget(discount)
self.list_layout.addWidget(card)
self.list_layout.addStretch()
def logout(self):
self.close()
def main():
app = QApplication(sys.argv)
while True:
login_window = LoginWindow()
if login_window.exec() != QDialog.DialogCode.Accepted:
break
products_window = ProductsWindow(login_window.user)
products_window.show()
app.exec()
# После закрытия списка возвращаемся к авторизации.
# Закрытие главного приложения завершает программу.
break
if __name__ == "__main__":
main()
```