Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save AspirantDrago/8958465d6d67a2ec8c4944eaff2ca49e to your computer and use it in GitHub Desktop.

Select an option

Save AspirantDrago/8958465d6d67a2ec8c4944eaff2ca49e to your computer and use it in GitHub Desktop.
import io
import csv
import sys
from PyQt6 import uic
from PyQt6 import QtCore, QtWidgets
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QWidget, QTableWidgetItem
FILENAME = 'price.csv'
template = """<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>974</width>
<height>491</height>
</rect>
</property>
<property name="font">
<font>
<family>MS Shell Dlg 2</family>
<pointsize>14</pointsize>
</font>
</property>
<property name="windowTitle">
<string>Интерактивный чек</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<widget class="QTableWidget" name="tableWidget"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Итого</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="total">
<property name="text">
<string>0</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
"""
class InteractiveReceipt(QWidget):
def __init__(self):
super().__init__()
f = io.StringIO(template)
uic.loadUi(f, self)
self._goods: list[str] = []
self._prices: list[int] = []
self._counts: list[int] = []
self.load_file()
self.show_data()
self.tableWidget.cellChanged.connect(self.cell_changed)
def cell_changed(self, row: int, column: int) -> None:
try:
count = int(self.tableWidget.item(row, column).text())
assert count >= 0
except BaseException:
self.tableWidget.item(row, column).setText(str(self._counts[row]))
else:
self._counts[row] = count
self.calc()
def load_file(self) -> None:
with open(FILENAME, encoding='utf-8') as csvfile:
reader = csv.reader(csvfile, delimiter=';')
next(reader)
for row in reader:
self._goods.append(row[0])
self._prices.append(int(row[1]))
self._counts.append(0)
def show_data(self) -> None:
self.tableWidget.clear()
self.tableWidget.setColumnCount(3)
self.tableWidget.setRowCount(len(self._goods))
for i, (g, p, c) in enumerate(zip(self._goods, self._prices, self._counts)):
goods_item = QTableWidgetItem(g)
price_item = QTableWidgetItem(str(p))
count_item = QTableWidgetItem(str(c))
goods_item.setFlags(~Qt.ItemFlag.ItemIsEditable)
price_item.setFlags(~Qt.ItemFlag.ItemIsEditable)
self.tableWidget.setItem(i, 0, goods_item)
self.tableWidget.setItem(i, 1, price_item)
self.tableWidget.setItem(i, 2, count_item)
self.calc()
def calc(self) -> None:
summa = sum(
price * count
for price, count in zip(self._prices, self._counts)
)
self.total.setText(str(summa))
def except_hook(cls, exception, traceback):
sys.__excepthook__(cls, exception, traceback)
if __name__ == '__main__':
if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
sys.excepthook = except_hook
app = QApplication(sys.argv)
ex = InteractiveReceipt()
ex.show()
sys.exit(app.exec())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment