## Ex 3-1. 창 띄우기.
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()
self.detailView = QHBoxLayout()
self.detailView.addWidget(self.canvas)
self.detailMenus = []
for i in range(10):
vvvv = QPushButton()
self.detailMenus.append(vvvv)
self.detailHori.addWidget(vvvv)
vQ.addLayout(self.detailView)
vQ.setStretchFactor(self.detailHori, 1)
vQ.setStretchFactor(self.canvas, 20)
tabs.addTab(self.tableWidget, 'Table')
tabs.addTab(self.canvas, 'Graph')
hori02.addWidget(tabs)
layout.addLayout(hori01)
layout.addLayout(self.detailHori)
layout.addLayout(hori02)
layout.setStretchFactor(hori01, 2)
layout.setStretchFactor(self.detailHori, 2)
layout.setStretchFactor(hori02, 8)
return layout
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)
x = list(x)
# x.pop()
db.append(x)
f.close()
height = len(db)
width = len(db[0])
self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
for i in range(height):
for j in range(width):
self.tableWidget.setItem(i, j, QTableWidgetItem(str(db[i][j])))
# 0, 3, 8
x = []
y1 = []
y2 = []
for i in range(len(db[0])):
self.detailMenus[i].setText(db[1][i])
if len(self.detailMenus[i].text()) == 0:
self.detailMenus[i].setDisabled(True)
for i in range(2, len(db)):
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()
self.canvas.draw()
if name == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
#----------------------------------------------------
#
# import csv
# import copy
#
# f = open('data2.csv', 'r', encoding='ansi')
# rdr = csv.reader(f)
# db = []
# for line in rdr:
# print(line, type(line), len(line))
# x = copy.deepcopy(line)
# x = list(x)
# # x.pop()
# db.append(x)
#
# f.close()
#
# print(len(db), len(db[0]))
# for i in range(len(db)):
# for j in range(len(db[10])):
# try:
# db[i][j] = float(db[i][j])
# except:
# x = 1
# # if i>=2 and ( j!=0 and j !=4 and j != 9 ):
# # db[i][j] = float(db[i][j])
# print(db[i][j], end=' ')
# print()
#
# xData = []
# yData = []
# aData = []
# bData = []
# for i in range(len(db)):
# yData.append(db[i][3])
# xData.append(db[i][0])
# aData.append(db[i][5])
# bData.append(db[i][8])
# print(db[i][0], db[i][3], db[i][5], db[i][8])
#
# import matplotlib.pyplot as plt
# import numpy as np
# print(xData)
# xData.pop(0)
# xData.pop(0)
# yData.pop(0)
# yData.pop(0)
# aData.pop(0)
# aData.pop(0)
# bData.pop(0)
# bData.pop(0)
#
# x = []
# y = []
# for i in range(len(xData)):
# x.append(i+1)
#
# for i in range(len(aData)):
# y.append(i+1)
#
# plt.plot(x, yData, 'bo')
# plt.plot(y, bData, color='#e35f62', marker='*', linewidth=2)
#
#
# plt.xlabel('Time(s)')
# plt.ylabel('Current(mA)')
# plt.legend('I')
# plt.legend('I2')
#
# plt.tick_params(axis='both', direction='in', length=3, pad=6, labelsize=14)
# plt.title('Current vs pH')
#
# plt.show()