class GoodsGuiHandler(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) self.controller = DbController() # запускаем настройки self.setUp__addItemBtn() self.setUp__delItemBtn() self.setUp__listWidget() def setUp__addItemBtn(self): self.addItemBtn.clicked.connect(self.addItemToListEval) def setUp__delItemBtn(self): self.delItemBtn.clicked.connect(self.delItemsFromListEval) def setUp__listWidget(self): goods = self.controller.getList() if goods: self.listWidget.addItems(goods) ########################################################################### def addItemToListEval(self): text = self.itemLE.text() if text: self.listWidget.addItem(text) self.controller.addItem(text) self.itemLE.clear() def delItemsFromListEval(self): self.controller.deleteItem(self.listWidget.currentItem().text()) self.listWidget.takeItem(self.listWidget.currentRow())
class MainScreen(FloatLayout): def __init__(self, **kwargs): super(MainScreen, self).__init__(**kwargs) self.controller = DbController() #подписи над полями self.add_widget(Label(text='Список товаров', size_hint=(.4, .05), pos_hint={'x':.0, 'y':.95})) self.add_widget(Label(text='Введите новый товар', size_hint=(.4, .05), pos_hint={'x':.4, 'y':.95})) self.add_widget(Label(text='Введите количество', size_hint=(.2, .05), pos_hint={'x':.8, 'y':.95})) # поле ввода товара self.goodTi = TextInput(multiline=False, size_hint=(.4, .05), pos_hint={'x':.4, 'y':.9}) self.add_widget(self.goodTi) # кнокпка добавления self.addGoodBtn = Button(text='Добавить товар', size_hint=(.4, .05), pos_hint={'x':.4, 'y':.85}) self.add_widget(self.addGoodBtn) self.addGoodBtn.bind(on_press=self.addGoodBtnCallback) # инпут для ввода количества self.quantitySpin = FloatInput(size_hint=(.2, .05), pos_hint={'x':.8, 'y':.9}) self.add_widget(self.quantitySpin) self.list_view = RV(size_hint=(.4, .9), pos_hint={'x':.0, 'y':.05}) self.add_widget(self.list_view) # кнокпка добавления self.delGoodBtn = Button(text='Удалить товар', size_hint=(.4, .05), pos_hint={'x':.0, 'y':.0}) self.add_widget(self.delGoodBtn) self.delGoodBtn.bind(on_press=self.delGoodBtnCallback) self.popup = Popup(title='Не хватает данных', content=Label(text='Для добавления в список введите название товара и его количество'), size_hint=(None, None), size=(600, 200) ) self.setUp__listWidget() def addGoodBtnCallback(self, instance): if self.goodTi.text and self.quantitySpin.text: good = self.goodTi.text quantity = self.quantitySpin.text self.goodTi.text = '' self.quantitySpin.text = '' item = self.controller.addItem(good, quantity) self.list_view.replaceItemByName(item['item'], item['quantity']) self.list_view.refresh_from_data() else: self.popup.open() def delGoodBtnCallback(self, instance): itemLine = self.list_view.delSelectedItem() itemName = re.sub(r'[,\.0-9+-]+', '', itemLine) self.controller.deleteItem(itemName) def setUp__listWidget(self): goods = self.controller.getList() print(goods) if goods: for good in goods: self.list_view.addItem(good['item'], good['quantity'])