# -*- coding: utf-8 -*标签:checkBox,进制,Form,win,self,计算,print From: https://blog.51cto.com/u_15773623/5972896
import ctypes
import struct
import time
from PyQt5 import QtCore, QtGui
from PyQt5.QtCore import QEventLoop, QTimer, QThread
from PyQt5.QtGui import QColor
from untitled19 import Ui_Form
import sys
import fraction
# from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QColorDialog
class Cmdzl():
def xf(self):
try:
self.run1()
self.run2()
self.run3()
self.run4()
self.run5()
self.run6()
self.run7()
self.run8()
time.sleep(0.5)
col2 = QColor(255,0,0)
win.pushButton.setStyleSheet("QWidget { background-color: %s }" % col2.name())
except:
print("输入格式错误")
def run1(self):
if win.checkBox.isChecked() == True:
t = win.lineEdit.text()
if win.comboBox.currentText()=='16进制转10进制':
t16 = (int(int(t, 16)))
print(t,"(的16进制转10进制)=", t16)
elif win.comboBox.currentText()=='16进制转2进制':
t16 = (int(int(t, 16)))
t16_2 = bin(t16)
print(t, "(的16进制转 2进制)=", t16_2)
elif win.comboBox.currentText() == '16进制转ASSCII':
t16 = (int(int(t, 16)))
t16_ASS = chr(int(t16))
print(t, "(16进制对应的ASCII码字符)=",t16_ASS)
elif win.comboBox.currentText() == '10进制转16进制':
col2 = QColor(0, 255, 0)
win.label_3.setStyleSheet("QWidget { background-color: %s }" % col2.name())
t10 = (int(int(t, 10)))
t10_16 = hex(t10)
print(t, "(的10进制转16进制)=", t10_16)
elif win.comboBox.currentText() == '10进制转2进制':
t10 = (int(int(t, 10)))
t10_2 = bin(t10)[2:]
print(t, "(的10进制转 2进制)=", t10_2)
elif win.comboBox.currentText() == '10进制转ASSCII':
tAS = chr(int(t))
print(t, "(10进制对应的ASCII码字符)=", tAS)
elif win.comboBox.currentText() == '2进制转10进制':
t2 = int(t, 2)
print(t, "(2进制的10进制)=", t2)
elif win.comboBox.currentText() == '2进制转16进制':
t2_10 = int(t, 2)
t2_16 = hex(t2_10)
print(t, "(2进制的16进制)=", t2_16)
elif win.comboBox.currentText() == '2进制转ASSCII':
t2_10 = int(t, 2)
t2_ASSCII = chr(int(t2_10))
print(t, "(2进制对应的ASCII码字符为)=",t2_ASSCII)
elif win.comboBox.currentText() == 'ASCII码转10进制':
ASCII_10 = ord(t)
print(t, "(ASCII码转10进制)=", ASCII_10)
elif win.comboBox.currentText() == 'ASCII码转16进制':
ASCII_10 = ord(t)
ASCCII_16 = hex(ASCII_10)
print(t, "(ASCII码转16进制)=", ASCCII_16)
elif win.comboBox.currentText() == 'ASCII码转2进制':
ASCII_10 = ord(t)
ASSCII_2 = bin(ASCII_10)
print(t, "(ASCII码转 2进制)=", ASSCII_2)
elif win.comboBox.currentText() == '小数转单精度':
d=self.float_to_hex(float(t)).upper()
print(t, "(小数转单精度)=", d)
elif win.comboBox.currentText() == '小数转双精度':
d = self.double_to_hex(float(t)).upper()
print(t, "(小数转双精度)=", d)
elif win.comboBox.currentText() == 'hex单精度转小数':
d = self.hex2float(str(t))
print(t, "(16进制单精度转小数)=", d)
elif win.comboBox.currentText() == 'hex双精度转小数':
d = self.hexToDouble(str(t))
print(t, "(16进制双精度转小数)=", d)
else:
pass
def float_to_hex(self, f):
return hex(struct.unpack('<I', struct.pack('<f', f))[0])
def double_to_hex(self, f):
return hex(struct.unpack('<Q', struct.pack('<d', f))[0])
def hex2float(self,h):
i = int(h, 16)
cp = ctypes.pointer(ctypes.c_int(i))
fp = ctypes.cast(cp, ctypes.POINTER(ctypes.c_float))
return fp.contents.value
def hexToDouble(self,hexString): # 16进制转double
return struct.unpack('>d', bytes.fromhex(hexString))[0]
def run2(self):
if win.checkBox_2.isChecked() == True:
t = win.lineEdit.text()
if win.comboBox_2.currentText() =="法拉(F)":
t1 = int(t)
t2 = t1 * 1000
t3 = t1 * 1000* 1000
t4 = t1 * 1000 * 1000* 1000
t5 = t1 * 1000 * 1000 * 1000* 1000
print(t, "法拉(F)", '=', t2, '毫法(mF)')
print(t, "法拉(F)", '=', t3, '微法(μF)')
print(t, "法拉(F)", '=', t4, '纳法(nF)')
print(t, "法拉(F)", '=', t5, '皮法(pF)')
if win.comboBox_2.currentText() == "毫法(mF)":
t1 = int(t)
t2 = t1 / 1000
t3 = t1 * 1000
t4 = t1 * 1000 * 1000
t5 = t1 * 1000 * 1000 * 1000
print(t, "毫法(mF)", '=', t2, '法拉(F)')
print(t, "毫法(mF)", '=', t3, '微法(μF)')
print(t, "毫法(mF)", '=', t4, '纳法(nF)')
print(t, "毫法(mF)", '=', t5, '皮法(pF)')
if win.comboBox_2.currentText() == "微法(μF)":
t1 = int(t)
t2 = t1 / (1000 * 1000)
t3 = t1 / 1000
t4 = t1 * 1000
t5 = t1 * 1000 * 1000
print(t, "微法(μF)", '=', t2, '法拉(F)')
print(t, "微法(μF)", '=', t3, '毫法(mF)')
print(t, "微法(μF)", '=', t4, '纳法(nF)')
print(t, "微法(μF)", '=', t5, '皮法(pF)')
if win.comboBox_2.currentText() == "纳法(nF)":
t1 = int(t)
t2 = t1 / (1000 * 1000 * 1000)
t3 = t1 / (1000 * 1000)
t4 = t1 / 1000
t5 = t1 * 1000
d = '纳法(nF)'
print(t, d, '=', t2, '法拉(F)')
print(t, d, '=', t3, '毫法(mF)')
print(t, d, '=', t4, '微法(μF)')
print(t, d, '=', t5, '皮法(pF)')
if win.comboBox_2.currentText() == "皮法(pF)":
t1 = int(t)
t2 = t1 / (1000 * 1000 * 1000 * 1000)
t3 = t1 / (1000 * 1000 * 1000)
t4 = t1 / (1000 * 1000)
t5 = t1 / 1000
d = '皮法(pF)'
print(t, d, '=', t2, '法拉(F)')
print(t, d, '=', t3, '毫法(mF)')
print(t, d, '=', t4, '微法(μF)')
print(t, d, '=', t5, '纳法(nF)')
print('\n')
def run3(self):
if win.checkBox_5.isChecked() == True:
t = win.lineEdit.text()
t2 = win.lineEdit_2.text()
t3 = float(t) + float(t2)
print(t, "+", t2, '=', t3)
def run4(self):
if win.checkBox_6.isChecked() == True:
t = win.lineEdit.text()
t2 = win.lineEdit_2.text()
t3 = float(t) - float(t2)
print(t, "-", t2, '=', t3)
def run5(self):
if win.checkBox_7.isChecked() == True:
t = win.lineEdit.text()
t2 = win.lineEdit_2.text()
t3 = float(t) * float(t2)
print(t, "×", t2, '=', t3)
def run6(self):
if win.checkBox_8.isChecked() == True:
t = win.lineEdit.text()
t2 = win.lineEdit_2.text()
t3 = float(t) / float(t2)
print(t, "÷", t2, '=', t3)
def run7(self):
if win.checkBox_3.isChecked() == True:
t = float(win.lineEdit.text())
t2 =int( win.lineEdit_2.text())
# print(t,'的',t2, "次方")
t3 =pow(t, t2)
print(t,'(的)',t2, "(次方)", '=', t3)
def run8(self):
if win.checkBox_4.isChecked() == True:
t = float(win.lineEdit.text())
t2 =int(win.lineEdit_2.text())
t3 =float(1/t2)
t4 =pow(t, t3)
print(t,"(的根号"
""
""
""
")",t2 ,"(次方)", '=', t4)
class ControlBoard(QMainWindow, Ui_Form):
def __init__(self):
super(ControlBoard, self).__init__()
self.setupUi(self)
# 下面将输出重定向到textBrowser中
sys.stdout = EmittingStr(textWritten=self.outputWritten)
sys.stderr = EmittingStr(textWritten=self.outputWritten)
self.pushButton.clicked.connect(self.bClicked)
self.checkBox_9.stateChanged.connect(self.run20)
self.initUI()
def initUI(self):
self.下拉框()
self.下拉框2()
self.coclor()
def outputWritten(self, text):
cursor = self.textEdit_2.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(text)
self.textEdit_2.setTextCursor(cursor)
self.textEdit_2.ensureCursorVisible()
def bClicked(self):
t = runThread(self)
t.start()
self.textEdit_2.setText("")
def coclor(self):
# col = QColor(255, 0, 0)
self.btn4 = self.pushButton
self.btn4.clicked.connect(self.showDialog4)
# self.pushButton.setStyleSheet("QWidget { background-color: %s }" % col.name())
def showDialog4(self):
col = QColor(0, 255,0)
start_time = time.time()
if col.isValid():
self.pushButton.setStyleSheet("QWidget { background-color: %s }" % col.name())
def run20(self):
if win.checkBox_9.isChecked() == False:
self.checkBox.setChecked(False)
self.checkBox_2.setChecked(False)
self.checkBox_3.setChecked(False)
self.checkBox_4.setChecked(False)
self.checkBox_5.setChecked(False)
self.checkBox_6.setChecked(False)
self.checkBox_7.setChecked(False)
self.checkBox_8.setChecked(False)
else:
self.checkBox.setChecked(True)
def 下拉框(self):
combo = self.comboBox
combo.addItem("16进制转10进制")
combo.addItem("16进制转2进制")
combo.addItem("16进制转ASSCII")
combo.addItem("10进制转16进制")
combo.addItem("10进制转2进制")
combo.addItem("10进制转ASSCII")
combo.addItem("2进制转10进制")
combo.addItem("2进制转16进制")
combo.addItem("2进制转ASSCII")
combo.addItem("ASCII码转10进制")
combo.addItem("ASCII码转16进制")
combo.addItem("ASCII码转2进制")
combo.addItem("小数转单精度")
combo.addItem("小数转双精度")
combo.addItem("hex单精度转小数")
combo.addItem("hex双精度转小数")
combo.activated[str].connect(self.onActivated)
def 下拉框2(self):
combo2 = self.comboBox_2
combo2.addItem("法拉(F)")
combo2.addItem("毫法(mF)")
combo2.addItem("微法(μF)")
combo2.addItem("纳法(nF)")
combo2.addItem("皮法(pF)")
combo2.activated[str].connect(self.onActivated)
def onActivated(self, text):
self.label_3.setText(text)
self.label_3.adjustSize()
# self.textEdit.setText(text)
def exceptOutConfig(exctype, value, tb):
print('My Error Information:')
print('Type:', exctype)
print('Value:', value)
print('Traceback:', tb)
class EmittingStr(QtCore.QObject):
textWritten = QtCore.pyqtSignal(str) # 定义一个发送str的信号
def write(self, text):
self.textWritten.emit(str(text))
class runThread(QThread):
def run(self):
Cmdzl().xf()
# win.pushButton.setEnabled(True)
if __name__ == "__main__":
sys.excepthook = exceptOutConfig
app = QApplication(sys.argv)
win = ControlBoard()
win.show()
Cmdzl()
sys.exit(app.exec_())
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled19.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(346, 229)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(80, 30, 54, 12))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(280, 10, 41, 20))
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(10, 10, 121, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(142, 10, 131, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.checkBox = QtWidgets.QCheckBox(Form)
self.checkBox.setGeometry(QtCore.QRect(10, 40, 71, 16))
self.checkBox.setObjectName("checkBox")
self.comboBox = QtWidgets.QComboBox(Form)
self.comboBox.setGeometry(QtCore.QRect(10, 60, 121, 22))
self.comboBox.setObjectName("comboBox")
self.checkBox_2 = QtWidgets.QCheckBox(Form)
self.checkBox_2.setGeometry(QtCore.QRect(10, 90, 71, 16))
self.checkBox_2.setObjectName("checkBox_2")
self.comboBox_2 = QtWidgets.QComboBox(Form)
self.comboBox_2.setGeometry(QtCore.QRect(10, 110, 121, 22))
self.comboBox_2.setObjectName("comboBox_2")
self.checkBox_3 = QtWidgets.QCheckBox(Form)
self.checkBox_3.setGeometry(QtCore.QRect(10, 180, 71, 16))
self.checkBox_3.setObjectName("checkBox_3")
self.checkBox_4 = QtWidgets.QCheckBox(Form)
self.checkBox_4.setGeometry(QtCore.QRect(60, 180, 71, 16))
self.checkBox_4.setObjectName("checkBox_4")
self.checkBox_5 = QtWidgets.QCheckBox(Form)
self.checkBox_5.setGeometry(QtCore.QRect(10, 140, 71, 16))
self.checkBox_5.setObjectName("checkBox_5")
self.checkBox_6 = QtWidgets.QCheckBox(Form)
self.checkBox_6.setGeometry(QtCore.QRect(60, 140, 71, 16))
self.checkBox_6.setObjectName("checkBox_6")
self.checkBox_7 = QtWidgets.QCheckBox(Form)
self.checkBox_7.setGeometry(QtCore.QRect(10, 160, 71, 16))
self.checkBox_7.setObjectName("checkBox_7")
self.checkBox_8 = QtWidgets.QCheckBox(Form)
self.checkBox_8.setGeometry(QtCore.QRect(60, 160, 71, 16))
self.checkBox_8.setObjectName("checkBox_8")
self.checkBox_9 = QtWidgets.QCheckBox(Form)
self.checkBox_9.setGeometry(QtCore.QRect(10, 200, 71, 16))
self.checkBox_9.setObjectName("checkBox_9")
self.textEdit_2 = QtWidgets.QTextEdit(Form)
self.textEdit_2.setGeometry(QtCore.QRect(140, 40, 201, 121))
self.textEdit_2.setObjectName("textEdit_2")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(140, 200, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(140, 170, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(230, 170, 75, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(240, 210, 54, 12))
self.label_3.setObjectName("label_3")
self.retranslateUi(Form)
self.pushButton_2.clicked.connect(self.textEdit_2.clear) # type: ignore
self.pushButton_3.clicked.connect(Form.close) # type: ignore
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "计算器"))
self.label.setText(_translate("Form", "A"))
self.label_2.setText(_translate("Form", "B"))
self.checkBox.setText(_translate("Form", "进制转换"))
self.checkBox_2.setText(_translate("Form", "电容"))
self.checkBox_3.setText(_translate("Form", "平方"))
self.checkBox_4.setText(_translate("Form", "平方根"))
self.checkBox_5.setText(_translate("Form", "A+B"))
self.checkBox_6.setText(_translate("Form", "A-B"))
self.checkBox_7.setText(_translate("Form", "A×B"))
self.checkBox_8.setText(_translate("Form", "A÷B"))
self.checkBox_9.setText(_translate("Form", "反选"))
self.pushButton.setText(_translate("Form", "结果="))
self.pushButton_2.setText(_translate("Form", "清除信息"))
self.pushButton_3.setText(_translate("Form", "退出"))
self.label_3.setText(_translate("Form", "author liuzhen"))
self.lineEdit_2.setText('2')
self.lineEdit.setText('4')