pyqt 的溶剂比例计算器 snail
[总点击:662次]
2009年11月10日
RT
开始做植化了,要用,鑫哥的那个是 for win 的,以前写了个 ruby-gtk 的不知道扔哪里去了,于是重新写了个 pyqt 的。
ps: 图形界面的程序真是浪费表情阿,真正干活的就两三行,一旦要图形界面了就几百行,这个看来是死要面子(图形界面)活受罪的真实写照阿,呵呵。
片片:

源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | #!/usr/bin/python # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'snail.ui' # # Created: Tue Nov 10 17:44:47 2009 # by: PyQt4 UI code generator 4.6.1 # # WARNING! All changes made in this file will be lost! import sys, os from PyQt4 import QtCore, QtGui from PyQt4.QtGui import QMainWindow from PyQt4.QtCore import pyqtSignature class Ui_mainWindow(object): def setupUi(self, mainWindow): mainWindow.setObjectName("mainWindow") mainWindow.resize(767, 375) self.centralwidget = QtGui.QWidget(mainWindow) self.centralwidget.setObjectName("centralwidget") self.textEdit = QtGui.QTextEdit(self.centralwidget) self.textEdit.setGeometry(QtCore.QRect(170, 90, 171, 51)) self.textEdit.setStyleSheet("font: 22pt \"Bitstream Vera Sans Mono\";") self.textEdit.setObjectName("textEdit") self.textEdit_2 = QtGui.QTextEdit(self.centralwidget) self.textEdit_2.setGeometry(QtCore.QRect(380, 90, 171, 51)) self.textEdit_2.setStyleSheet("font: 22pt \"Bitstream Vera Sans Mono\";") self.textEdit_2.setObjectName("textEdit_2") self.textEdit_3 = QtGui.QTextEdit(self.centralwidget) self.textEdit_3.setGeometry(QtCore.QRect(590, 90, 161, 51)) self.textEdit_3.setStyleSheet("font: 22pt \"Bitstream Vera Sans Mono\";") self.textEdit_3.setObjectName("textEdit_3") self.textEdit_4 = QtGui.QTextEdit(self.centralwidget) self.textEdit_4.setGeometry(QtCore.QRect(170, 180, 171, 51)) self.textEdit_4.setStyleSheet("font: 22pt \"Bitstream Vera Sans Mono\";") self.textEdit_4.setObjectName("textEdit_4") self.textEdit_5 = QtGui.QTextEdit(self.centralwidget) self.textEdit_5.setGeometry(QtCore.QRect(380, 180, 171, 51)) self.textEdit_5.setStyleSheet("font: 22pt \"Bitstream Vera Sans Mono\";") self.textEdit_5.setObjectName("textEdit_5") self.label = QtGui.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(200, 20, 131, 41)) self.label.setStyleSheet("font: 26pt \"Bitstream Vera Sans Mono\"; 溶剂 A") self.label.setObjectName("label") self.label_2 = QtGui.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(410, 20, 131, 41)) self.label_2.setStyleSheet("font: 26pt \"Bitstream Vera Sans Mono\"; 溶剂 A") self.label_2.setObjectName("label_2") self.label_3 = QtGui.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(630, 20, 131, 41)) self.label_3.setStyleSheet("font: 26pt \"Bitstream Vera Sans Mono\"; 溶剂 A") self.label_3.setObjectName("label_3") self.label_4 = QtGui.QLabel(self.centralwidget) self.label_4.setGeometry(QtCore.QRect(20, 90, 151, 41)) self.label_4.setStyleSheet("font: 26pt \"Bitstream Vera Sans Mono\"; 溶剂 A") self.label_4.setObjectName("label_4") self.label_5 = QtGui.QLabel(self.centralwidget) self.label_5.setGeometry(QtCore.QRect(20, 190, 151, 41)) self.label_5.setStyleSheet("font: 26pt \"Bitstream Vera Sans Mono\"; 溶剂 A") self.label_5.setObjectName("label_5") self.pushButton = QtGui.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(590, 180, 161, 51)) self.pushButton.setStyleSheet("font: 20pt \"Bitstream Vera Sans Mono\";") self.pushButton.setObjectName("pushButton") self.label_6 = QtGui.QLabel(self.centralwidget) self.label_6.setGeometry(QtCore.QRect(30, 270, 721, 71)) self.label_6.setStyleSheet("""font: 22pt \"Bitstream Vera Sans Mono\"; color: rgb(0, 0, 255);""") self.label_6.setObjectName("label_6") self.frame = QtGui.QFrame(self.centralwidget) self.frame.setEnabled(True) self.frame.setGeometry(QtCore.QRect(20, 260, 731, 91)) self.frame.setMouseTracking(False) self.frame.setAutoFillBackground(False) self.frame.setFrameShape(QtGui.QFrame.StyledPanel) self.frame.setFrameShadow(QtGui.QFrame.Raised) self.frame.setObjectName("frame") mainWindow.setCentralWidget(self.centralwidget) self.menubar = QtGui.QMenuBar(mainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 767, 25)) self.menubar.setObjectName("menubar") mainWindow.setMenuBar(self.menubar) self.statusbar = QtGui.QStatusBar(mainWindow) self.statusbar.setObjectName("statusbar") mainWindow.setStatusBar(self.statusbar) self.retranslateUi(mainWindow) #QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.label_6.) QtCore.QMetaObject.connectSlotsByName(mainWindow) def retranslateUi(self, mainWindow): mainWindow.setWindowTitle(QtGui.QApplication.translate("mainWindow", "溶剂比例计算器 -Snail", None, QtGui.QApplication.UnicodeUTF8)) self.textEdit_3.setHtml(QtGui.QApplication.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:\'Bitstream Vera Sans Mono\'; font-size:22pt; 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;\">1000</p></body></html>", None, QtGui.QApplication.UnicodeUTF8)) self.label.setText(QtGui.QApplication.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:\'Bitstream Vera Sans Mono\'; font-size:26pt; 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;\">溶剂 A</p></body></html>", None, QtGui.QApplication.UnicodeUTF8)) self.label_2.setText(QtGui.QApplication.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:\'Bitstream Vera Sans Mono\'; font-size:26pt; 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;\">溶剂 B</p></body></html>", None, QtGui.QApplication.UnicodeUTF8)) self.label_3.setText(QtGui.QApplication.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:\'Bitstream Vera Sans Mono\'; font-size:26pt; 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;\">体积</p></body></html>", None, QtGui.QApplication.UnicodeUTF8)) self.label_4.setText(QtGui.QApplication.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:\'Bitstream Vera Sans Mono\'; font-size:26pt; 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;\">现有溶液</p></body></html>", None, QtGui.QApplication.UnicodeUTF8)) self.label_5.setText(QtGui.QApplication.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:\'Bitstream Vera Sans Mono\'; font-size:26pt; 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;\">欲配溶液</p></body></html>", None, QtGui.QApplication.UnicodeUTF8)) self.pushButton.setText(QtGui.QApplication.translate("mainWindow", "也来算一个!", None, QtGui.QApplication.UnicodeUTF8)) self.label_6.setText(QtGui.QApplication.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:\'Bitstream Vera Sans Mono\'; font-size:22pt; 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;\">欢迎使用溶剂比例计算器 Snail</p>\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Xiooli <xioooli@yahoo.com.cn></p></body></html>", None, QtGui.QApplication.UnicodeUTF8)) class Win(QMainWindow, Ui_mainWindow): def __init__(self, parent = None): QMainWindow.__init__(self, parent) self.setupUi(self) @pyqtSignature("") def on_pushButton_clicked(self): self.label_6_txt = "" try: now_A = float(self.textEdit.toPlainText()) now_B = float(self.textEdit_2.toPlainText()) now_vol = float(self.textEdit_3.toPlainText()) want_A = float(self.textEdit_4.toPlainText()) want_B = float(self.textEdit_5.toPlainText()) except ValueError: self.label_6_txt = "错误:有输入框未输入数字,或输入了非数字字符!" if not self.label_6_txt: if want_A*(now_A+now_B) == 0 or want_B*(now_A+now_B) == 0: self.label_6_txt = "错误:试图除零!" elif now_A / now_B > want_A / want_B: vol,rst=self.textEdit_3.toPlainText(),\ str(round(now_vol*now_A*(want_A+want_B)/(want_A*(now_A+now_B))-now_vol,4)) self.label_6_txt = "须向 " + vol + " ML 现有溶液中加入溶液 B " + rst + " ML" elif now_A / now_B == want_A / want_B: self.label_6_txt = "现有溶液和欲配溶液是同一种溶液,无须额外动作。" else: vol,rst=self.textEdit_3.toPlainText(),\ str(round(now_vol*now_B*(want_A+want_B)/(want_B*(now_A+now_B))-now_vol,4)) self.label_6_txt = "须向 " + vol + " ML 现有溶液中加入溶液 A " + rst + " ML" self.label_6.setText(QtGui.QApplication.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:\'Bitstream Vera Sans Mono\'; font-size:22pt; 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;\">"+self.label_6_txt+"</p></body></html>", None, QtGui.QApplication.UnicodeUTF8)) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) win=Win() win.show() sys.exit(app.exec_()) |
这写得也太累了,全人工布局,要个几百行还真正常,虽然是pyqt,但好歹布局管理器也有吧,还字体属性等,定义一个模板出来,然后赋值不就好了。。那么多code,看得我心惊肉跳
呵呵,看起来是吓人哈,其实那个是用qt-designer自动生成的,就是用鼠标拖放控件,画好了以后转换成python代码,手工写的也就十几行吧。