import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLineEdit, QPushButton, QTabWidget, \
QTableWidget, QFileDialog, QAbstractItemView, QHeaderView, QTableWidgetItem
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import csv
import copy
class MyApp(QWidget):
def init(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('First Programmm v.0.1')
self.resize(1000, 800)
self.fig = plt.Figure()
self.canvas = FigureCanvas(self.fig)
self.setLayout(self.backgroundLayout())
def backgroundLayout(self):
layout = QVBoxLayout()
hori01 = QHBoxLayout()
hori02 = QHBoxLayout()
self.addressInputLine = QLineEdit()
self.addressInputLine.setDisabled(True)
self.addressInputButton = QPushButton('Open')
self.addressInputButton.clicked.connect(self.openFile)
hori01.addWidget(self.addressInputLine)
hori01.addWidget(self.addressInputButton)
hori01.setStretchFactor(self.addressInputLine, 5)
hori01.setStretchFactor(self.addressInputButton, 1)
tabs = QTabWidget()
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(100)
self.tableWidget.setColumnCount(10)
vQ = QVBoxLayout()
self.detailHori = QHBoxLayout() # menus
self.detailView = QHBoxLayout() # Image
self.detailView.addWidget(self.canvas)
self.detailMenus = []
self.detailSelected = []
for i in range(10):
vvvv = QPushButton()
vvvv.setStyleSheet('background-color: white;')
self.detailMenus.append(vvvv)
self.detailHori.addWidget(vvvv)
self.detailSelected.append(0)
self.detailMenus[0].clicked.connect(lambda: self.optionClicked(0))
self.detailMenus[1].clicked.connect(lambda: self.optionClicked(1))
self.detailMenus[2].clicked.connect(lambda: self.optionClicked(2))
self.detailMenus[3].clicked.connect(lambda: self.optionClicked(3))
self.detailMenus[4].clicked.connect(lambda: self.optionClicked(4))
self.detailMenus[5].clicked.connect(lambda: self.optionClicked(5))
self.detailMenus[6].clicked.connect(lambda: self.optionClicked(6))
self.detailMenus[7].clicked.connect(lambda: self.optionClicked(7))
self.detailMenus[8].clicked.connect(lambda: self.optionClicked(8))
self.detailMenus[9].clicked.connect(lambda: self.optionClicked(9))
self.detailMenu2 = QHBoxLayout() # Make a Layout
self.reloadBtn = QPushButton('ReLoad')
self.reloadBtn.clicked.connect(self.reload)
self.detailMenu2.addWidget(self.reloadBtn)
tabs.addTab(self.tableWidget, 'Table')
tabs.addTab(self.canvas, 'Graph')
hori02.addWidget(tabs)
layout.addLayout(hori01)
layout.addLayout(self.detailHori)
layout.addLayout(self.detailMenu2)
layout.addLayout(hori02)
layout.setStretchFactor(hori01, 2)
layout.setStretchFactor(self.detailHori, 2)
layout.setStretchFactor(hori02, 8)
return layout
def optionClicked(self, loc):
if self.detailSelected[loc] == 0:
self.detailSelected[loc] = 1
self.detailMenus[loc].setStyleSheet('background-color: green;')
elif self.detailSelected[loc] == 1:
self.detailSelected[loc] = 2
self.detailMenus[loc].setStyleSheet('background-color: red;')
else:
self.detailSelected[loc] = 0
self.detailMenus[loc].setStyleSheet('background-color: white;')
def openFile(self):
fname = QFileDialog.getOpenFileName(self)
self.addressInputLine.setText(fname[0])
# address is [ self.addressInputLine.text() ]
link = self.addressInputLine.text()
f = open(link, 'r', encoding='ansi')
rdr = csv.reader(f)
db = []
for line in rdr:
print(line, type(line), len(line))
x = copy.deepcopy(line)
db.append(x)
f.close()
#########################################
height = len(db)
width = len(db[0])
self.tableWidget.setRowCount(height)
self.tableWidget.setColumnCount(width)
self.tableWidget.setEditTriggers(QAbstractItemView.AllEditTriggers) #수정 불가 옵션
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) #Stretch 모드
for i in range(height):
for j in range(width):
self.tableWidget.setItem(i, j, QTableWidgetItem(str(db[i][j]))) # 각 칸의 값을 저장
x = []
y1 = []
y2 = []
for i in range(len(db[0])):
self.detailMenus[i].setText(db[1][i]) # 버튼의 제목
if len(self.detailMenus[i].text()) == 0: # Text 에 아무 것도 없는 칸은 비활성화
self.detailMenus[i].setDisabled(True)
for i in range(2, len(db)): # Data X, Y1 Y2에 input 명령
x.append(int(db[i][0]))
y1.append(float(db[i][3]))
y2.append(float(db[i][8]))
self.fig.clear() # 그래프 초기화
ax = self.fig.add_subplot(111) # 하나 만들어서 하나 쓸꺼야
ax.plot(x, y1, label=db[1][0])
ax.plot(x, y2, label=db[1][8], linestyle="--")
ax.set_xlabel("x")
ax.set_xlabel("y")
ax.set_title(self.addressInputLine.text().split('/')[-1])
ax.legend()
# 1, 4, 8
self.detailMenus[0].setStyleSheet('background-color: green;')
self.detailMenus[3].setStyleSheet('background-color: green;')
self.detailMenus[8].setStyleSheet('background-color: green;')
self.detailSelected[0] = 1
self.detailSelected[3] = 1
self.detailSelected[8] = 1
self.canvas.draw()
###################################
def reload(self):
print('selected menu number is')
greenOption = 0
greenTable = []
redOption = 0
redTable = 0
for i in range(len(self.detailSelected)):
if self.detailSelected[i] == 1:
greenOption += 1
greenTable.append(i)
elif self.detailSelected[i] == 2:
redOption += 1
redTable = i
if redOption > 1:
print('Red Option is to many')
return
else:
print('')
try:
x = []
# setItem(0, 0, QTableWidgetItem("1행 1열"))
# 출처: https://spec.tistory.com/442 [Code Hunter:티스토리]
# self.tableWidget.setItem(5, 5, QTableWidgetItem("1행 1열"))
# print(self.tableWidget.rowCount(), self.tableWidget.columnCount())
for i in range(2, self.tableWidget.rowCount()):
x.append(int(self.tableWidget.item(i, redTable+1).text()))
print(x, redTable)
y = []
print(greenTable)
for i in range(greenOption):
print(greenTable[i])
dummy = []
for j in range(2, self.tableWidget.rowCount()):
# print(type(self.tableWidget.item(i+1, greenTable[i]+1).text()))
dummy.append(float(self.tableWidget.item(i+1, greenTable[i]+1).text()))
print(dummy)
except:
print('?')
# for i in range(2, len(db)): # Data X, Y1 Y2에 input 명령
# x.append(int(db[i][0]))
if name == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())