Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import os
- import sys
- import sqlite3 as db
- from PyQt4 import QtCore, QtGui, QtSql, Qt
- from PyQt4.QtCore import QStringList, QString
- from PyQt4.QtGui import QApplication, QMainWindow, QSizePolicy
- from PyQt4.QtGui import QTableWidget, QTableWidgetItem
- import re
- import config
- id_number = config.id_number
- db_filename = config.db_filename
- try:
- _fromUtf8 = QtCore.QString.fromUtf8
- except AttributeError:
- _fromUtf8 = lambda s: s
- class MainWindow(QtGui.QWidget):
- def __init__(self, child):
- super(MainWindow, self).__init__()
- self.setObjectName(_fromUtf8("self"))
- self.resize(826, 518)
- self.verticalLayout = QtGui.QVBoxLayout(self)
- self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
- self.tableWidget_cards = QtGui.QTableWidget(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.tableWidget_cards.setFont(font)
- self.tableWidget_cards.setObjectName(_fromUtf8("tableWidget_cards"))
- self.tableWidget_cards.setColumnCount(0)
- self.tableWidget_cards.setRowCount(0)
- self.verticalLayout.addWidget(self.tableWidget_cards)
- self.horizontalLayout = QtGui.QHBoxLayout()
- self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
- self.Add_Button = QtGui.QPushButton(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.Add_Button.setFont(font)
- self.Add_Button.setObjectName(_fromUtf8("Add_Button"))
- self.horizontalLayout.addWidget(self.Add_Button)
- self.Change_Button = QtGui.QPushButton(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.Change_Button.setFont(font)
- self.Change_Button.setObjectName(_fromUtf8("Change_Button"))
- self.horizontalLayout.addWidget(self.Change_Button)
- self.Delete_Button = QtGui.QPushButton(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.Delete_Button.setFont(font)
- self.Delete_Button.setObjectName(_fromUtf8("Delete_Button"))
- self.horizontalLayout.addWidget(self.Delete_Button)
- self.Close_Button = QtGui.QPushButton(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.Close_Button.setFont(font)
- self.Close_Button.setObjectName(_fromUtf8("Close_Button"))
- self.horizontalLayout.addWidget(self.Close_Button)
- self.verticalLayout.addLayout(self.horizontalLayout)
- self.retranslateUi()
- QtCore.QObject.connect(self.Close_Button, QtCore.SIGNAL(_fromUtf8("clicked()")), self.close)
- QtCore.QMetaObject.connectSlotsByName(self)
- QtCore.QObject.connect(self.Delete_Button, QtCore.SIGNAL(_fromUtf8("clicked()")), self.delete)
- self.setTabOrder(self.Add_Button, self.Change_Button)
- self.setTabOrder(self.Change_Button, self.Delete_Button)
- self.setTabOrder(self.Delete_Button, self.Close_Button)
- self.setTabOrder(self.Close_Button, self.tableWidget_cards)
- self.Add_Button.clicked.connect(child.show)
- self.data = 'OK'
- self.update()
- def retranslateUi(self):
- self.setWindowTitle(QtGui.QApplication.translate("self", "Товары", None, QtGui.QApplication.UnicodeUTF8))
- self.Add_Button.setText(QtGui.QApplication.translate("self", "Добавить", None, QtGui.QApplication.UnicodeUTF8))
- self.Change_Button.setText(QtGui.QApplication.translate("self", "Изменить", None, QtGui.QApplication.UnicodeUTF8))
- self.Delete_Button.setText(QtGui.QApplication.translate("self", "Удалить", None, QtGui.QApplication.UnicodeUTF8))
- self.Close_Button.setText(QtGui.QApplication.translate("self", "Закрыть", None, QtGui.QApplication.UnicodeUTF8))
- def printData(self):
- print(self.data)
- self.update()
- def update(self):
- print "Update run"
- global db_filename
- self.tableWidget_cards.clear()
- self.tableWidget_cards.setColumnCount(6)
- self.tableWidget_cards.setRowCount(40) #выставить столько полей, сколько в таблице
- self.tableWidget_cards.setHorizontalHeaderLabels(
- [u'Производитель', u'Наименование', u'Ед. измерения', u'Ставка НДС', u'№РУ', u'Дата истечения'])
- conn = db.connect(db_filename)
- c = conn.cursor()
- db_is_new = not os.path.exists(db_filename)
- if db_is_new:
- print 'Need to create schema'
- else:
- print 'Database exists, assume schema does, too.'
- conn.execute(
- '''CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, manufacturer TEXT NOT NULL, name TEXT NOT NULL, unit TEXT, nds TEXT NOT NULL, ru TEXT NOT NULL, date_end TEXT);''')
- print "Table created successfully"
- i = 0
- rowd = 0
- for row in c.execute("SELECT * FROM cards"):
- a = [row[5], row[2], row[4], row[6], row[1], row[3]]
- for i in range(6):
- name = unicode(a[i])
- self.tableWidget_cards.setItem(rowd, i, QtGui.QTableWidgetItem(name))
- print name
- i += 1
- rowd += 1
- print rowd
- conn.close()
- self.tableWidget_cards.resizeColumnsToContents()
- print "Update end"
- def delete(self):
- print "Delete RUN"
- global db_filename
- cell = self.getsamerowcell()
- print cell
- conn = db.connect(db_filename)
- c = conn.cursor()
- c.execute('DELETE FROM cards WHERE name=?', (cell,))
- conn.commit()
- conn.close
- self.update()
- print "Delete END"
- def getsamerowcell(self):
- matchcol = 0
- row = self.tableWidget_cards.currentItem().row()
- headercount = self.tableWidget_cards.columnCount()
- for x in range(0,headercount,1):
- headertext = self.tableWidget_cards.horizontalHeaderItem(x).text()
- if u'Наименование' == unicode(headertext):
- matchcol = x
- break
- cell = self.tableWidget_cards.item(row,matchcol).text()
- print unicode(cell)
- return unicode(cell)
- class Child(QtGui.QWidget):
- def __init__(self):
- super(Child, self).__init__()
- self.mainWindow = None
- def setMainWindow(self, window):
- if self.mainWindow is not None:
- raise Exception('Failed!')
- self.mainWindow = window
- # button = QtGui.QPushButton(u'Пробуй', self)
- # button.setMinimumSize(QtCore.QSize(150, 70))
- # button.clicked.connect(self.mainWindow.printData)
- self.setObjectName(_fromUtf8("self"))
- self.resize(370, 577)
- self.setLocale(QtCore.QLocale(QtCore.QLocale.Russian, QtCore.QLocale.RussianFederation))
- self.verticalLayout = QtGui.QVBoxLayout(self)
- self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
- self.label = QtGui.QLabel(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.label.setFont(font)
- self.label.setObjectName(_fromUtf8("label"))
- self.verticalLayout.addWidget(self.label)
- self.manufac_lineEdit = QtGui.QLineEdit(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.manufac_lineEdit.setFont(font)
- self.manufac_lineEdit.setObjectName(_fromUtf8("manufac_lineEdit"))
- self.verticalLayout.addWidget(self.manufac_lineEdit)
- self.label_2 = QtGui.QLabel(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.label_2.setFont(font)
- self.label_2.setObjectName(_fromUtf8("label_2"))
- self.verticalLayout.addWidget(self.label_2)
- self.name_lineEdit = QtGui.QLineEdit(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.name_lineEdit.setFont(font)
- self.name_lineEdit.setObjectName(_fromUtf8("name_lineEdit"))
- self.verticalLayout.addWidget(self.name_lineEdit)
- self.label_3 = QtGui.QLabel(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.label_3.setFont(font)
- self.label_3.setObjectName(_fromUtf8("label_3"))
- self.verticalLayout.addWidget(self.label_3)
- self.unit_comboBox = QtGui.QComboBox(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.unit_comboBox.setFont(font)
- self.unit_comboBox.setObjectName(_fromUtf8("unit_comboBox"))
- self.unit_comboBox.addItem(_fromUtf8(""))
- self.unit_comboBox.addItem(_fromUtf8(""))
- self.unit_comboBox.addItem(_fromUtf8(""))
- self.unit_comboBox.addItem(_fromUtf8(""))
- self.unit_comboBox.addItem(_fromUtf8(""))
- self.unit_comboBox.addItem(_fromUtf8(""))
- self.verticalLayout.addWidget(self.unit_comboBox)
- self.label_4 = QtGui.QLabel(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.label_4.setFont(font)
- self.label_4.setObjectName(_fromUtf8("label_4"))
- self.verticalLayout.addWidget(self.label_4)
- self.NDS_comboBox = QtGui.QComboBox(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.NDS_comboBox.setFont(font)
- self.NDS_comboBox.setObjectName(_fromUtf8("NDS_comboBox"))
- self.NDS_comboBox.addItem(_fromUtf8(""))
- self.NDS_comboBox.addItem(_fromUtf8(""))
- self.NDS_comboBox.addItem(_fromUtf8(""))
- self.verticalLayout.addWidget(self.NDS_comboBox)
- self.label_6 = QtGui.QLabel(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.label_6.setFont(font)
- self.label_6.setObjectName(_fromUtf8("label_6"))
- self.verticalLayout.addWidget(self.label_6)
- self.RU_lineEdit = QtGui.QLineEdit(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.RU_lineEdit.setFont(font)
- self.RU_lineEdit.setObjectName(_fromUtf8("RU_lineEdit"))
- self.verticalLayout.addWidget(self.RU_lineEdit)
- self.label_5 = QtGui.QLabel(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.label_5.setFont(font)
- self.label_5.setObjectName(_fromUtf8("label_5"))
- self.verticalLayout.addWidget(self.label_5)
- self.calendarWidget = QtGui.QCalendarWidget(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.calendarWidget.setFont(font)
- self.calendarWidget.setObjectName(_fromUtf8("calendarWidget"))
- self.verticalLayout.addWidget(self.calendarWidget)
- self.Save_Button = QtGui.QPushButton(self)
- font = QtGui.QFont()
- font.setPointSize(12)
- self.Save_Button.setFont(font)
- self.Save_Button.setObjectName(_fromUtf8("Change_Button"))
- self.verticalLayout.addWidget(self.Save_Button)
- self.retranslateUi()
- QtCore.QMetaObject.connectSlotsByName(self)
- QtCore.QObject.connect(self.Save_Button, QtCore.SIGNAL(_fromUtf8("clicked()")), self.save)
- def retranslateUi(self):
- self.setWindowTitle(QtGui.QApplication.translate("self", "Карточка товара", None, QtGui.QApplication.UnicodeUTF8))
- self.label.setText(QtGui.QApplication.translate("self", "Прозводитель", None, QtGui.QApplication.UnicodeUTF8))
- self.label_2.setText(QtGui.QApplication.translate("self", "Наименование", None, QtGui.QApplication.UnicodeUTF8))
- self.label_3.setText(QtGui.QApplication.translate("self", "Единица измерения", None, QtGui.QApplication.UnicodeUTF8))
- self.unit_comboBox.setItemText(0, QtGui.QApplication.translate("self", "Упаковка", None, QtGui.QApplication.UnicodeUTF8))
- self.unit_comboBox.setItemText(1, QtGui.QApplication.translate("self", "Килограммы", None, QtGui.QApplication.UnicodeUTF8))
- self.unit_comboBox.setItemText(2, QtGui.QApplication.translate("self", "Штуки", None, QtGui.QApplication.UnicodeUTF8))
- self.unit_comboBox.setItemText(3, QtGui.QApplication.translate("self", "Литры", None, QtGui.QApplication.UnicodeUTF8))
- self.unit_comboBox.setItemText(4, QtGui.QApplication.translate("self", "Наборы", None, QtGui.QApplication.UnicodeUTF8))
- self.unit_comboBox.setItemText(5, QtGui.QApplication.translate("self", "Флаконы", None, QtGui.QApplication.UnicodeUTF8))
- self.label_4.setText(QtGui.QApplication.translate("self", "Ставка НДС", None, QtGui.QApplication.UnicodeUTF8))
- self.NDS_comboBox.setItemText(0, QtGui.QApplication.translate("self", "10", None, QtGui.QApplication.UnicodeUTF8))
- self.NDS_comboBox.setItemText(1, QtGui.QApplication.translate("self", "18", None, QtGui.QApplication.UnicodeUTF8))
- self.NDS_comboBox.setItemText(2, QtGui.QApplication.translate("self", "Без НДС", None, QtGui.QApplication.UnicodeUTF8))
- self.label_6.setText(QtGui.QApplication.translate("self", "№ РУ", None, QtGui.QApplication.UnicodeUTF8))
- self.label_5.setText(QtGui.QApplication.translate("self", "Дата окнчания РУ", None, QtGui.QApplication.UnicodeUTF8))
- self.Save_Button.setText(QtGui.QApplication.translate("self", "Сохранить", None, QtGui.QApplication.UnicodeUTF8))
- def save(self):
- global rowd, db_filename, id_number
- import sqlite3 as db
- print "Start"
- ru = unicode(self.RU_lineEdit.text())
- name = unicode(self.name_lineEdit.text())
- date_ends = self.calendarWidget.selectedDate()
- date_end = date_ends.toString()
- unit = unicode(self.unit_comboBox.currentText())
- nds = unicode(self.NDS_comboBox.currentText())
- manufacturer = unicode(self.manufac_lineEdit.text())
- db_is_new = not os.path.exists(db_filename)
- conn = db.connect(db_filename)
- c = conn.cursor()
- if db_is_new:
- print 'Need to create schema'
- else:
- print 'Database exists, assume schema does, too.'
- conn.execute(
- '''CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, manufacturer TEXT NOT NULL, name TEXT NOT NULL, unit TEXT, nds TEXT NOT NULL, ru TEXT NOT NULL, date_end TEXT);''')
- print "Table created successfully"
- print 'Inserting initial data'
- i = 0
- c.execute("SELECT id FROM cards")
- print "id_number = " + str(id_number)
- rows = c.fetchall()
- for row in rows:
- id_n = str(rows[i])
- id_n = re.search('(\d+)', id_n).group()
- print "id_n_from_rows = " + str(id_n)
- if id_number == 0:
- with conn:
- c.execute("Insert into cards (ru, name, date_end, unit, manufacturer, nds) values (?,?,?,?,?,?)",
- (ru, name, str(date_end), unit, manufacturer, nds))
- conn.commit
- break
- elif id_number == int(id_n):
- print "rows = " + str(rows[i])
- with conn:
- c.execute("UPDATE cards SET ru=?, name=?, date_end=?, unit=?, manufacturer=?, nds=? WHERE id=?",(ru, name, str(date_end), unit, manufacturer, id_number, nds))
- conn.commit
- id_number = 0
- break
- i+=1
- conn.close()
- self.manufac_lineEdit.clear()
- self.name_lineEdit.clear()
- self.RU_lineEdit.clear()
- id_number = 0
- print "Wrote"
- def closeEvent(self, event):
- self.mainWindow.printData()
- if __name__ == '__main__':
- app = QtGui.QApplication(sys.argv)
- child = Child()
- main = MainWindow(child)
- child.setMainWindow(main)
- main.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement