#!/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 from PyQt5.QtGui import QIcon from PyQt5.QtSql import * #import pymysql import dm # conn = pymysql.connect( # db='ats', # user='dba', # passwd='AlsprofilinE', # host='kis', # charset='utf8') # c = conn.cursor() class MainWin(QMainWindow): def __init__(self): super().__init__() self.initUI() #заглушка def qqqq(self): print("ddddddddd") 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) 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) self.statusBar() menubar = self.menuBar() fileMenu = menubar.addMenu('&Файл') fileMenu.addAction(newAction) fileMenu.addAction(deleteAction) fileMenu.addAction(printAction) fileMenu.addAction(exitAction) editMenu = menubar.addMenu('&Редактирование') editMenu.addAction(copyAction) editMenu.addAction(cutAction) editMenu.addAction(pasteAction) editMenu.addAction(printAction) 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) self.setGeometry(300, 300, 850, 650) self.setWindowTitle('Ацкый быдлокод') self.show() class MyTable(QTableWidget): def __init__(self, tbl, thestruct, *args): QTableWidget.__init__(self, *args) self.data = thestruct self.setmydata() self.tbl = tbl #print(self.tbl) n = 0 for item in dm.getTablesStructure(self.tbl): # установка заголовков столбцов таблицы self.setHorizontalHeaderItem(n, QTableWidgetItem(item)) n = n+1 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 class WorkArea(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): hbox = QHBoxLayout(self) # список таблиц listTablesWidget = QListWidget() listTablesWidget.setFrameShape(QFrame.StyledPanel) # таблица для вывода данных tablesDataWidget = QTableView() tablesDataWidget.setFrameShape(QFrame.StyledPanel) splitter1 = QSplitter(Qt.Horizontal) splitter1.addWidget(listTablesWidget) splitter1.addWidget(tablesDataWidget) splitter2 = QSplitter(Qt.Vertical) splitter2.addWidget(splitter1) hbox.addWidget(splitter2) self.setLayout(hbox) # выводим список таблиц в левом поле for i in dm.initDBstructure(): listTablesWidget.addItem(i[0]) # заполняем правый список данными из выбранной таблицы def addData(tbl_name): # удаляем предыдущий виджет таблицы item = splitter1.widget(1) if item is not None: item.deleteLater() data = dm.selectData(tbl_name) # проверка на наличие записей в таблице if data: # количество строк rows = len(data) # КОЛИЧЕСТВО КОЛОНОК cols = len(data[0]) table = MyTable(tbl_name, data, rows, cols) table.setFrameShape(QFrame.StyledPanel) splitter1.addWidget(table) # обработка нажатия мышой на списке #listTablesWidget.itemClicked.connect(lambda: dm.selectData(listTablesWidget.currentItem)) listTablesWidget.itemClicked.connect(lambda: addData(listTablesWidget.model().data(listTablesWidget.currentIndex()))) def onChanged(self, text): self.lbl.setText(text) self.lbl.adjustSize() if __name__ == '__main__': app = QApplication(sys.argv) ex = MainWin() sys.exit(app.exec_())