data-manipulation/gui.py

204 lines
6.7 KiB
Python
Raw Normal View History

2017-03-13 15:18:52 +03:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame,
QSplitter, QStyleFactory, QApplication)
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplication, QListView, QListWidget, QTableWidget , QTableView, QTableWidgetItem
2017-03-13 15:18:52 +03:00
from PyQt5.QtGui import QIcon
from PyQt5.QtSql import *
import pymysql
2017-03-13 15:18:52 +03:00
import dm
conn = pymysql.connect(
db='ats',
user='dba',
passwd='AlsprofilinE',
host='kis',
charset='utf8')
c = conn.cursor()
2017-03-13 15:18:52 +03:00
class MainWin(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
#заглушка
def qqqq(self):
print("ddddddddd")
2017-03-13 15:18:52 +03:00
def initUI(self):
textEdit = QTextEdit()
splitter = WorkArea()
self.setCentralWidget(splitter)
#self.setCentralWidget(textEdit)
newAction = QAction(QIcon('img/new.gif'), 'Добавить', self)
newAction.setShortcut('Ins')
newAction.setStatusTip('Добавить')
newAction.triggered.connect(self.qqqq)
deleteAction = QAction(QIcon('img/delete.gif'), 'Удалить', self)
deleteAction.setShortcut('Del')
deleteAction.setStatusTip('Удалить')
deleteAction.triggered.connect(self.qqqq)
exitAction = QAction(QIcon('img/exit.gif'), 'Выход', self)
2017-03-13 15:18:52 +03:00
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Выход')
exitAction.triggered.connect(self.close)
cutAction = QAction(QIcon('img/cut.gif'), 'Вырезать', self)
cutAction.setShortcut('Ctrl+X')
cutAction.setStatusTip('Вырезать')
#cutAction.triggered.connect(self.close)
copyAction = QAction(QIcon('img/copy.gif'), 'Копировать', self)
copyAction.setShortcut('Ctrl+С')
copyAction.setStatusTip('Копировать')
#copyAction.triggered.connect(self.close)
pasteAction = QAction(QIcon('img/paste.gif'), 'Вставить', self)
pasteAction.setShortcut('Ctrl+V')
pasteAction.setStatusTip('Вставить')
#pasteAction.triggered.connect(self.close)
findAction = QAction(QIcon('img/find.gif'), 'Копировать', self)
findAction.setShortcut('Ctrl+F')
findAction.setStatusTip('Искать')
# findAction.triggered.connect(self.close)
printAction = QAction(QIcon('img/print.gif'), 'Печатать', self)
printAction.setShortcut('Ctrl+P')
printAction.setStatusTip('Печатать')
# printAction.triggered.connect(self.close)
2017-03-13 15:18:52 +03:00
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&Файл')
fileMenu.addAction(newAction)
fileMenu.addAction(deleteAction)
fileMenu.addAction(printAction)
2017-03-13 15:18:52 +03:00
fileMenu.addAction(exitAction)
editMenu = menubar.addMenu('&Редактирование')
editMenu.addAction(copyAction)
editMenu.addAction(cutAction)
editMenu.addAction(pasteAction)
editMenu.addAction(printAction)
2017-03-13 15:18:52 +03:00
helpMenu = menubar.addMenu('&Помощь')
#toolbar = self.addToolBar('Редактирование')
toolbar = self.addToolBar('Панель инструментов')
toolbar.addAction(newAction)
toolbar.addAction(deleteAction)
toolbar.addAction(copyAction)
toolbar.addAction(cutAction)
toolbar.addAction(pasteAction)
toolbar.addAction(findAction)
toolbar.addAction(printAction)
#toolbar.addAction(exitAction)
2017-03-13 15:18:52 +03:00
self.setGeometry(300, 300, 850, 650)
self.setWindowTitle('Ацкый быдлокод')
self.show()
class MyTable(QTableWidget):
2017-03-14 17:01:30 +03:00
def __init__(self, tbl, thestruct, *args):
QTableWidget.__init__(self, *args)
self.data = thestruct
self.setmydata()
2017-03-14 17:01:30 +03:00
self.tbl = tbl
2017-03-14 17:01:30 +03:00
#print(self.tbl)
n = 0
2017-03-14 17:01:30 +03:00
for item in dm.getTablesStructure(self.tbl):
print(item)
# установка заголовков столбцов таблицы
#character = chr(ord('A') + n)
self.setHorizontalHeaderItem(n, QTableWidgetItem(item))
n = n+1
2017-03-14 17:01:30 +03:00
def setmydata(self):
n = 0
for key in self.data:
m = 0
for item in key:
newitem = QTableWidgetItem(item)
self.setItem(m, n, newitem)
n += 1
m += 1
2017-03-13 15:18:52 +03:00
class WorkArea(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
2017-03-13 16:50:33 +03:00
# список таблиц
listTablesWidget = QListWidget()
listTablesWidget.setFrameShape(QFrame.StyledPanel)
# таблица для вывода данных
tablesDataWidget = QTableView()
tablesDataWidget.setFrameShape(QFrame.StyledPanel)
2017-03-13 15:18:52 +03:00
splitter1 = QSplitter(Qt.Horizontal)
2017-03-13 16:50:33 +03:00
splitter1.addWidget(listTablesWidget)
splitter1.addWidget(tablesDataWidget)
2017-03-13 15:18:52 +03:00
splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(splitter1)
hbox.addWidget(splitter2)
self.setLayout(hbox)
2017-03-13 16:50:33 +03:00
# выводим список таблиц в левом поле
2017-03-13 15:18:52 +03:00
for i in dm.initDBstructure():
2017-03-14 17:01:30 +03:00
listTablesWidget.addItem(i[0])
2017-03-13 16:50:33 +03:00
# заполняем правый список данными из выбранной таблицы
def addData(tbl_name):
qwery = "SELECT * FROM " + tbl_name
c.execute(qwery)
# удаляем предыдущий виджет таблицы
item = splitter1.widget(1)
if item is not None:
#print(item)
item.deleteLater()
data = c.fetchall()
# проверка на наличие записей в таблице
if data:
# количество строк
rows = len(data)
# КОЛИЧЕСТВО КОЛОНОК
cols = len(data[0])
2017-03-14 17:01:30 +03:00
table = MyTable(tbl_name, data, rows, cols)
table.setFrameShape(QFrame.StyledPanel)
splitter1.addWidget(table)
2017-03-13 16:50:33 +03:00
# обработка нажатия мышой на списке
#listTablesWidget.itemClicked.connect(lambda: dm.selectData(listTablesWidget.currentItem))
listTablesWidget.itemClicked.connect(lambda: addData(listTablesWidget.model().data(listTablesWidget.currentIndex())))
2017-03-13 15:18:52 +03:00
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWin()
sys.exit(app.exec_())