大神可以帮我看看怎么把这个python代码利用ui运行呢
本帖最后由 v.n.lee 于 2019-7-2 19:35 编辑自己写了个计算粘度指数的python文件,想做个界面。在pyqt5里面做的效果图https://attach.52pojie.cn//forum/201907/02/172459fn8k5vzkekyqeekz.jpg?l,但是不知道怎么结合起来,看了好几本书还是不会弄{:1_906:}。求大佬棒棒忙
下面是py计算粘度的代码:
import math
v100 = input("100℃运动粘度:")
v100 = float(v100)
v40 = input("40℃运动粘度:")
v40 = float(v40)
if v100 < 3.8:
a = 1.14673
b = 1.7576
c = -0.109
d = 0.84155
e = 1.5521
f = -0.077
if 3.8 <= v100 < 4.4:
a = 3.38095
b = -15.4952
c = 33.196
d = 0.78571
e = 1.7929
f = -0.183
if 4.4 <= v100 < 5:
a = 2.5
b = -7.2143
c = 13.812
d = 0.82143
e = 1.5679
f = 0.119
if 5 <= v100 < 6.4:
a = 0.101
b = 16.635
c = -45.469
d = 0.04985
e = 9.1613
f = -18.557
if 6.4 <= v100 < 7:
a = 3.35714
b = -23.5643
c = 78.466
d = 0.22619
e = 7.7369
f = -16.656
if 7 <= v100 < 7.7:
a = 0.01191
b = 21.475
c = -72.87
d = 0.79762
e = -0.7321
f = 14.61
if 7.7 <= v100 < 9:
a = 0.41858
b = 16.1558
c = -56.04
d = 0.05794
e = 10.5156
f = -28.24
if 9 <= v100 < 12:
a = 0.88779
b = 7.5527
c = -16.6
d = 0.26665
e = 6.7015
f = -10.81
if 12 <= v100 < 15:
a = 0.7672
b = 10.7972
c = -38.18
d = 0.20073
e = 8.4658
f = -22.49
if 15 <= v100 < 18:
a = 0.97305
b = 5.3135
c = -2.2
d = 0.28889
e = 5.9741
f = -4.93
if 18 <= v100 < 22:
a = 0.97256
b = 5.25
c = -0.98
d = 0.24504
e = 7.416
f = -16.73
if 22 <= v100 < 28:
a = 0.91413
b = 7.4759
c = -21.82
d = 0.20323
e = 9.1267
f = -34.23
if 28 <= v100 < 40:
a = 0.87031
b = 9.7157
c = -50.77
d = 0.18411
e = 10.1015
f = -46.75
if 40 <= v100 < 55:
a = 0.84703
b = 12.6752
c = -133.31
d = 0.17029
e = 11.4866
f = -80.62
if 55 <= v100 < 70:
a = 0.85921
b = 11.1009
c = -83.19
d = 0.1713
e = 11.368
f = -76.94
if v100 >=70:
a = 0.83531
b = 14.6731
c = -216.246
d = 0.16841
e = 11.8493
f = -96.947
def l():
return a * v100 ** 2 + b * v100 + c
def h():
return d * v100 ** 2 + e * v100 + f
def n():
return (math.log(h()) - math.log(v40)) / math.log(v100)
def vi_1():
return (10 ** n() - 1) / 0.0075 + 100
def vi_2():
return ((l() - v40) / (l() - h())) * 100
if vi_1() <= 100:
print(round(vi_2()))
else:
print(round(vi_1()))
然后界面转的py代码:
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(225, 237)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.v40 = QtWidgets.QLineEdit(self.centralwidget)
self.v40.setGeometry(QtCore.QRect(70, 50, 113, 20))
self.v40.setObjectName("v40")
self.v100 = QtWidgets.QLineEdit(self.centralwidget)
self.v100.setGeometry(QtCore.QRect(70, 80, 113, 20))
self.v100.setObjectName("v100")
self.vi = QtWidgets.QLineEdit(self.centralwidget)
self.vi.setGeometry(QtCore.QRect(70, 110, 113, 20))
self.vi.setObjectName("vi")
self.calculate = QtWidgets.QPushButton(self.centralwidget)
self.calculate.setGeometry(QtCore.QRect(110, 140, 75, 23))
self.calculate.setObjectName("calculate")
self.splitter = QtWidgets.QSplitter(self.centralwidget)
self.splitter.setGeometry(QtCore.QRect(10, 50, 61, 81))
self.splitter.setOrientation(QtCore.Qt.Vertical)
self.splitter.setObjectName("splitter")
self.label_v40 = QtWidgets.QLabel(self.splitter)
self.label_v40.setObjectName("label_v40")
self.label_v100 = QtWidgets.QLabel(self.splitter)
self.label_v100.setObjectName("label_v100")
self.label_vi = QtWidgets.QLabel(self.splitter)
self.label_vi.setObjectName("label_vi")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 225, 23))
self.menubar.setObjectName("menubar")
self.menu = QtWidgets.QMenu(self.menubar)
self.menu.setObjectName("menu")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
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.calculate.setText(_translate("MainWindow", "计算"))
self.label_v40.setText(_translate("MainWindow", "40℃粘度"))
self.label_v100.setText(_translate("MainWindow", "100℃粘度"))
self.label_vi.setText(_translate("MainWindow", "粘度指数"))
self.menu.setTitle(_translate("MainWindow", "粘度指数计算器"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_()) 输入 40℃与100℃粘度后计算得出粘度指数 liphily 发表于 2019-7-2 17:42
要自定义函数名,然后把控件的动作改为目标函数名——
网上是这样说的,我也用pyqt画的——
就是各种报错 ...
翻了几本书了也没搞懂。网上也没有搜到类似的例子。还是缺乏系统的学习啊
本帖最后由 panwanpeng 于 2019-7-2 17:58 编辑
v.n.lee 发表于 2019-7-2 17:48
翻了几本书了也没搞懂。网上也没有搜到类似的例子。还是缺乏系统的学习啊
看我的帖子,文字识别那个帖子{:1_918:}
第一个代码是功能代码,第二个代码是画出来的界面代码,第三个代码是结合起来的代码。 把你的功能封装成函数,然后将函数绑定给UI的按键就可以了
panwanpeng 发表于 2019-7-2 17:56
看我的帖子,文字识别那个帖子
第一个代码是功能代码,第二个代码是画出来的界面代码,第三个 ...
谢谢大佬,学习学习 海若0101 发表于 2019-7-2 19:23
把你的功能封装成函数,然后将函数绑定给UI的按键就可以了
搞完这个我就去系统的学习一下
页:
[1]