import os import platform import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * import numpy as np import matplotlib matplotlib.use("Qt5Agg") from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.figure import Figure #注意不是pyplot下的那个figure class Table(QWidget): def __init__(self): super().__init__() hhbox = QHBoxLayout() #横向布局 self.tableWidget = QTableWidget() #创建一个表格 self.tableWidget.setRowCount(5) self.tableWidget.setColumnCount(4) #5行4列 self.tableWidget.setHorizontalHeaderLabels(['第一行列','第二列','第三列','第四列']) self.tableWidget.setVerticalHeaderLabels(['第一行','第二行','第三行','第四行','第五行']) #表头 # 往表格里添加东西 #nu self.tableWidget.setItem(0,0,QTableWidgetItem("1")) self.tableWidget.setItem(0,1,QTableWidgetItem("2")) self.tableWidget.setItem(0,2,QTableWidgetItem("3")) self.tableWidget.setItem(0,3,QTableWidgetItem("3")) self.tableWidget.setItem(1,0,QTableWidgetItem("7")) self.tableWidget.setItem(1,1,QTableWidgetItem("5")) self.tableWidget.setItem(1,2,QTableWidgetItem("6")) self.tableWidget.setItem(1,3,QTableWidgetItem("7")) self.tableWidget.setItem(2,0,QTableWidgetItem("8")) self.tableWidget.setItem(2,1,QTableWidgetItem("9")) self.tableWidget.setItem(2,2,QTableWidgetItem("10")) self.tableWidget.setItem(2,3,QTableWidgetItem("11")) self.tableWidget.setItem(3,0,QTableWidgetItem("4")) self.tableWidget.setItem(3,1,QTableWidgetItem("2")) self.tableWidget.setItem(3,2,QTableWidgetItem("3")) self.tableWidget.setItem(3,3,QTableWidgetItem("1")) self.tableWidget.setItem(4,0,QTableWidgetItem("8")) self.tableWidget.setItem(4,1,QTableWidgetItem("9")) self.tableWidget.setItem(4,2,QTableWidgetItem("10")) self.tableWidget.setItem(4,3,QTableWidgetItem("5")) hhbox.addWidget(self.tableWidget) #把表格加入布局 self.setLayout(hhbox) #创建布局 self.setWindowTitle("表格") self.resize(600,250) self.show() class Draw(QDialog): def __init__(self,parent=None): super().__init__(parent) self.table = Table() self.ok = QPushButton() self.ok.setText('画图') #绘图 self.fig = Figure(figsize=(3, 3), dpi=100) self.figureCanvas = FigureCanvas(self.fig) self.graphicscene = QGraphicsScene() self.graphicscene.addWidget(self.figureCanvas) self.graphicview = QGraphicsView() self.graphicview.setScene(self.graphicscene) #self.setCentralWidget(self.graphicview) layout = QGridLayout() layout.addWidget(self.table, 0, 0) layout.addWidget(self.ok, 1, 0) layout.addWidget(self.graphicview, 0, 1) self.setLayout(layout) self.ok.clicked.connect(self.draw_pic) def draw_pic(self): #获取选中的项目 item = self.table.tableWidget.selectedItems() ls=[] ''' for i in item: ls.extend(self.table.tableWidget.indexFromItem(i).data()) print(self.table.tableWidget.indexFromItem(i).data()) ''' for i in item: a=self.table.tableWidget.indexFromItem(i).row() #print(a) #print(dir(self.table.tableWidget.indexFromItem(i))) b=self.table.tableWidget.indexFromItem(i).column() #print(b) #print(self.table.tableWidget.indexFromItem(i).data()) ls.append(int(self.table.tableWidget.item(a,b).text())) print(ls) #显示 self.fig.clf() self.ax = self.fig.add_subplot(1,1,1) self.ax.plot(ls) self.figureCanvas.draw() pass if __name__ == "__main__": ''' app = QApplication(sys.argv) dlg = Example() sys.exit(app.exec_()) ''' app = QApplication(sys.argv) form = Draw() form.setMinimumSize(1000,400) form.show() app.exec_()