from untitled import Ui_MainWindow from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtCore, QtGui, QtWidgets import sys # from functools import partial import win32api,win32con, win32ui import xlrd,xlwt flag=0 #判断是否读入文件 # def convert(ui): # input = ui.lineEdit.text() # result = float(input) * 6.7 # ui.lineEdit_2.setText(str(result)) def adjust(*args): #屏幕自适应 screenSize=2 if(not args): f = open('setting.txt', 'r', encoding='utf8') try:#文件可能为空 screenSize=float(f.readline().strip()) except: pass f.close() else: screenSize=args[0] global window window.resize(win32api.GetSystemMetrics(win32con.SM_CXSCREEN)/screenSize, win32api.GetSystemMetrics(win32con.SM_CYSCREEN)/screenSize) def openXLS(filename): length=len(filename) if filename[length-5:length]== ".xlsx" or filename[length-4:length]==".xls": global nrow, ncol,sheet,flag flag=1 rbook=xlrd.open_workbook(filename)#打开文件 sheet = rbook.sheet_by_index(0) # 打开对应的表 nrow = sheet.nrows ncol = sheet.ncols # 找到行列总数 model= QStandardItemModel(nrow, ncol) # model.setHorizontalHeaderLabels(['0','1','2','3'])#设置表头 for row in range(nrow): for col in range(ncol): item = QStandardItem(str(sheet.cell_value(row,col))) model.setItem(row, col, item) ui.tableView.setModel(model) else: ui.textBrowser.setText("不支持打开该文件") def importFile(q): # print(q.text(), "llala") #以下是打开文件对话框以及获取文件名的方法 dlg = win32ui.CreateFileDialog(1) # dlg.SetOFNInitialDir('D:/Work') # 设置打开文件对话框中的初始显示目录,无效 dlg.DoModal() filename = dlg.GetPathName() # 获取选择的文件名称 openXLS(filename) def setWindowSize(q): f=open('setting.txt','w',encoding='utf8') if q.text()=="S": adjust(2) f.write('2\n') if q.text()=="M": adjust(1.5) f.write('1.5\n') if q.text()=="L": adjust(1.2) f.write('1.2\n') f.close() class Opration(object): global sheet def getSum(self): sum=0 for row in range(sheet.nrows): for col in range(sheet.ncols): sum+=sheet.cell_value(row,col) ui.textBrowser.setText("总和 = %s"%sum) def drawTendency(self): import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, sheet.nrows,sheet.nrows) y=[] for row in range(sheet.nrows): for col in range(sheet.ncols): y.append( sheet.cell_value(row, col)) plt.plot(x, y) plt.legend() plt.show() def addRatioButton(self): # vlayout = QVBoxLayout() # vlayout.addWidget(QPushButton(str(3))) # vlayout.addWidget(QPushButton(str(4))) # vwg = QWidget() # vwg.setLayout(vlayout) pass def op(opchoice): if flag==0: #判断表格是否读入 ui.textBrowser.setText("请导入数据") return text = opchoice.text() opr=Opration() if text=="getSum": opr.getSum() if text=="drawTendency": opr.drawTendency() if text=="addRatioButton": opr.addRatioButton() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv)#生成应用 window = QtWidgets.QMainWindow()#生成窗口q ui = Ui_MainWindow()#使用QTdesigner自动创建的类 ui.setupUi(window) adjust() window.show() # ui.pushButton.clicked.connect(getSum)#点击按钮后,触发时间convert,ui作为convert的参数 ui.menuFile.triggered[QAction].connect(importFile)#菜单触发 ui.menuWindow.triggered[QAction].connect(setWindowSize)#菜单触发调整窗口大小 ui.menu.triggered[QAction].connect(op) # 菜单触发调整窗口大小 sys.exit(app.exec_())
untitle.py(以下由QT designer生成)
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'untitled.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindowMainWindow") MainWindow.resize(552, 336) MainWindow.setStyleSheet("background-color:#efe") self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.horizontalLayout_7 = QtWidgets.QHBoxLayout(self.centralwidget) self.horizontalLayout_7.setObjectName("horizontalLayout_7") self.tableView = QtWidgets.QTableView(self.centralwidget) self.tableView.setObjectName("tableView") self.horizontalLayout_7.addWidget(self.tableView) self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget) self.textBrowser.setObjectName("textBrowser") self.horizontalLayout_7.addWidget(self.textBrowser) MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setStyleSheet("background:#eee") self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 552, 18)) self.menubar.setStyleSheet("background-color:#eee") self.menubar.setObjectName("menubar") self.menuFile = QtWidgets.QMenu(self.menubar) self.menuFile.setStyleSheet("color:red") self.menuFile.setObjectName("menuFile") self.menuWindow = QtWidgets.QMenu(self.menubar) self.menuWindow.setStyleSheet("color:blue") self.menuWindow.setObjectName("menuWindow") self.menu = QtWidgets.QMenu(self.menubar) self.menu.setObjectName("menu") MainWindow.setMenuBar(self.menubar) self.action1 = QtWidgets.QAction(MainWindow) self.action1.setObjectName("action1") self.actionimport = QtWidgets.QAction(MainWindow) self.actionimport.setObjectName("actionimport") self.actions = QtWidgets.QAction(MainWindow) self.actions.setObjectName("actions") self.actionM = QtWidgets.QAction(MainWindow) self.actionM.setObjectName("actionM") self.actionL = QtWidgets.QAction(MainWindow) self.actionL.setObjectName("actionL") self.actiongetSUM = QtWidgets.QAction(MainWindow) self.actiongetSUM.setObjectName("actiongetSUM") self.actionget = QtWidgets.QAction(MainWindow) self.actionget.setObjectName("actionget") self.actionaddRationButton = QtWidgets.QAction(MainWindow) self.actionaddRationButton.setObjectName("actionaddRationButton") self.menuFile.addAction(self.actionimport) self.menuWindow.addAction(self.actions) self.menuWindow.addAction(self.actionM) self.menuWindow.addAction(self.actionL) self.menu.addAction(self.actiongetSUM) self.menu.addAction(self.actionget) self.menu.addAction(self.actionaddRationButton) self.menubar.addAction(self.menuFile.menuAction()) self.menubar.addAction(self.menuWindow.menuAction()) self.menubar.addAction(self.menu.menuAction()) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.textBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n" "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" "p, li { white-space: pre-wrap; }\n" "</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Welcome</p></body></html>")) self.menuFile.setTitle(_translate("MainWindow", "File")) self.menuWindow.setTitle(_translate("MainWindow", "Window")) self.menu.setTitle(_translate("MainWindow", "操作")) self.action1.setText(_translate("MainWindow", "1")) self.actionimport.setText(_translate("MainWindow", "import")) self.actions.setText(_translate("MainWindow", "S")) self.actionM.setText(_translate("MainWindow", "M")) self.actionL.setText(_translate("MainWindow", "L")) self.actiongetSUM.setText(_translate("MainWindow", "getSum")) self.actionget.setText(_translate("MainWindow", "drawTendency")) self.actionaddRationButton.setText(_translate("MainWindow", "addRationButton"))