工作需要,频繁进行hex-str的转换,不想一直访问网站,做一个简单的UI
主要有三个UI:进制转换/hex-str/主界面
UI文件转换为py文件,使用main.py把他们关联起来
直接上代码,界面的py是使用"pyuic5 -o xx.py xx.ui"转换过来的:
进制转换baseconversion.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'base_conversion.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# 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_baseconversion(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 597)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Main_UI = QtWidgets.QPushButton(self.centralwidget)
self.Main_UI.setGeometry(QtCore.QRect(230, 390, 75, 23))
self.Main_UI.setObjectName("Main_UI")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(30, 200, 481, 161))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.oct_bin = QtWidgets.QPushButton(self.layoutWidget)
self.oct_bin.setObjectName("oct_bin")
self.gridLayout.addWidget(self.oct_bin, 0, 1, 1, 1)
self.dec_bin = QtWidgets.QPushButton(self.layoutWidget)
self.dec_bin.setObjectName("dec_bin")
self.gridLayout.addWidget(self.dec_bin, 0, 2, 1, 1)
self.hex_bin = QtWidgets.QPushButton(self.layoutWidget)
self.hex_bin.setObjectName("hex_bin")
self.gridLayout.addWidget(self.hex_bin, 0, 3, 1, 1)
self.bin_dec = QtWidgets.QPushButton(self.layoutWidget)
self.bin_dec.setObjectName("bin_dec")
self.gridLayout.addWidget(self.bin_dec, 1, 0, 1, 1)
self.oct_dec = QtWidgets.QPushButton(self.layoutWidget)
self.oct_dec.setObjectName("oct_dec")
self.gridLayout.addWidget(self.oct_dec, 1, 1, 1, 1)
self.dec_oct = QtWidgets.QPushButton(self.layoutWidget)
self.dec_oct.setObjectName("dec_oct")
self.gridLayout.addWidget(self.dec_oct, 1, 2, 1, 1)
self.hex_oct = QtWidgets.QPushButton(self.layoutWidget)
self.hex_oct.setObjectName("hex_oct")
self.gridLayout.addWidget(self.hex_oct, 1, 3, 1, 1)
self.oct_hex = QtWidgets.QPushButton(self.layoutWidget)
self.oct_hex.setObjectName("oct_hex")
self.gridLayout.addWidget(self.oct_hex, 2, 1, 1, 1)
self.dec_hex = QtWidgets.QPushButton(self.layoutWidget)
self.dec_hex.setObjectName("dec_hex")
self.gridLayout.addWidget(self.dec_hex, 2, 2, 1, 1)
self.hex_dec = QtWidgets.QPushButton(self.layoutWidget)
self.hex_dec.setObjectName("hex_dec")
self.gridLayout.addWidget(self.hex_dec, 2, 3, 1, 1)
self.bin_hex = QtWidgets.QPushButton(self.layoutWidget)
self.bin_hex.setObjectName("bin_hex")
self.gridLayout.addWidget(self.bin_hex, 2, 0, 1, 1)
self.bin_oct = QtWidgets.QPushButton(self.layoutWidget)
self.bin_oct.setObjectName("bin_oct")
self.gridLayout.addWidget(self.bin_oct, 0, 0, 1, 1)
self.layoutWidget1 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget1.setGeometry(QtCore.QRect(30, 70, 491, 111))
self.layoutWidget1.setObjectName("layoutWidget1")
self.gridLayout_2 = QtWidgets.QGridLayout(self.layoutWidget1)
self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
self.gridLayout_2.setObjectName("gridLayout_2")
self.label = QtWidgets.QLabel(self.layoutWidget1)
self.label.setObjectName("label")
self.gridLayout_2.addWidget(self.label, 0, 0, 1, 1)
self.lineEdit = QtWidgets.QLineEdit(self.layoutWidget1)
self.lineEdit.setObjectName("lineEdit")
self.gridLayout_2.addWidget(self.lineEdit, 0, 1, 1, 1)
self.Empty = QtWidgets.QPushButton(self.layoutWidget1)
self.Empty.setObjectName("Empty")
self.gridLayout_2.addWidget(self.Empty, 0, 2, 1, 1)
self.label_2 = QtWidgets.QLabel(self.layoutWidget1)
self.label_2.setObjectName("label_2")
self.gridLayout_2.addWidget(self.label_2, 1, 0, 1, 1)
self.lineEdit_2 = QtWidgets.QLineEdit(self.layoutWidget1)
self.lineEdit_2.setObjectName("lineEdit_2")
self.gridLayout_2.addWidget(self.lineEdit_2, 1, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.Main_UI.setText(_translate("MainWindow", "Main_UI"))
self.oct_bin.setText(_translate("MainWindow", "oct_bin"))
self.dec_bin.setText(_translate("MainWindow", "dec_bin"))
self.hex_bin.setText(_translate("MainWindow", "hex_bin"))
self.bin_dec.setText(_translate("MainWindow", "bin_dec"))
self.oct_dec.setText(_translate("MainWindow", "oct_dec"))
self.dec_oct.setText(_translate("MainWindow", "dec_oct"))
self.hex_oct.setText(_translate("MainWindow", "hex_oct"))
self.oct_hex.setText(_translate("MainWindow", "oct_hex"))
self.dec_hex.setText(_translate("MainWindow", "dec_hex"))
self.hex_dec.setText(_translate("MainWindow", "hex_dec"))
self.bin_hex.setText(_translate("MainWindow", "bin_hex"))
self.bin_oct.setText(_translate("MainWindow", "bin_oct"))
self.label.setText(_translate("MainWindow", "Please Input"))
self.Empty.setText(_translate("MainWindow", "Empty"))
self.label_2.setText(_translate("MainWindow", "Output"))
16进制和字符串转换hexstr.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '.\hexstr.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# 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_hexstr(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(608, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 62, 78, 16))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(100, 62, 381, 20))
self.lineEdit.setObjectName("lineEdit")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 100, 36, 16))
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(100, 100, 381, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(80, 160, 320, 25))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.str_hex = QtWidgets.QPushButton(self.layoutWidget)
self.str_hex.setObjectName("str_hex")
self.horizontalLayout.addWidget(self.str_hex)
self.hex_str = QtWidgets.QPushButton(self.layoutWidget)
self.hex_str.setObjectName("hex_str")
self.horizontalLayout.addWidget(self.hex_str)
self.Main_Ui = QtWidgets.QPushButton(self.centralwidget)
self.Main_Ui.setGeometry(QtCore.QRect(170, 250, 75, 23))
self.Main_Ui.setObjectName("Main_Ui")
self.Empty = QtWidgets.QPushButton(self.centralwidget)
self.Empty.setGeometry(QtCore.QRect(510, 60, 75, 23))
self.Empty.setObjectName("Empty")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 608, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Please input "))
self.label_2.setText(_translate("MainWindow", "Output"))
self.str_hex.setText(_translate("MainWindow", "str_hex"))
self.hex_str.setText(_translate("MainWindow", "hex_str"))
self.Main_Ui.setText(_translate("MainWindow", "Main_Ui"))
self.Empty.setText(_translate("MainWindow", "Empty"))
主界面mainInterface.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainInterface.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# 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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.Hex_Str = QtWidgets.QPushButton(Dialog)
self.Hex_Str.setGeometry(QtCore.QRect(60, 80, 75, 23))
self.Hex_Str.setObjectName("Hex_Str")
self.Base_Conversion = QtWidgets.QPushButton(Dialog)
self.Base_Conversion.setGeometry(QtCore.QRect(230, 80, 75, 23))
self.Base_Conversion.setObjectName("Base_Conversion")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.Hex_Str.setText(_translate("Dialog", "Hex_Str"))
self.Base_Conversion.setText(_translate("Dialog", "Base_Conversion"))
main.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import binascii
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow,QDialog
from hexstr import Ui_hexstr
from baseconversion import Ui_baseconversion
from PyQt5.QtCore import pyqtSignal
from mainInterface import Ui_Dialog
class MainInterfaceUI(QDialog,Ui_Dialog):
def __init__(self):
super(MainInterfaceUI, self).__init__()
self.setupUi(self)
self.Hex_Str.clicked.connect(self.tohexstr)
self.Base_Conversion.clicked.connect(self.tobaseconver)
def tohexstr(self):
self.hide()
self.s = Hex_StrUi()
self.s.show()
def tobaseconver(self):
self.hide()
self.s=Base_Conversion()
self.s.show()
class Hex_StrUi(QMainWindow,Ui_hexstr):
def __init__(self):
super(Hex_StrUi, self).__init__()
self.setupUi(self)
self.Main_Ui.clicked.connect(self.tomain) # 返回主页面
self.hex_str.clicked.connect(self.hex__str)
self.str_hex.clicked.connect(self.str__hex)
self.Empty.clicked.connect(self.empty)
def empty(self):
self.lineEdit.setText("")
def hex__str(self):
s = self.lineEdit.text()
le=len(s)
st=''
for i in range(0,le,2):
su=int(s[i],16)*16+int(s[i+1],16)
t=chr(su)
st+=t
self.lineEdit_2.setText(str(st))
def str__hex(self):
s = self.lineEdit.text()
str_bin=s.encode('utf-8')
st=binascii.hexlify(str_bin).decode('utf-8')
self.lineEdit_2.setText(str(st))
def tomain(self):
self.hide()
self.f =MainInterfaceUI()
self.f.show()
class Base_Conversion(QMainWindow, Ui_baseconversion):
def __init__(self):
super(Base_Conversion, self).__init__()
self.setupUi(self)
self.Main_UI.clicked.connect(self.tomain)
self.oct_bin.clicked.connect(self.oct__bin)
self.dec_bin.clicked.connect(self.dec__bin)
self.hex_bin.clicked.connect(self.hex__bin)
self.bin_oct.clicked.connect(self.bin__oct)
self.dec_oct.clicked.connect(self.dec__oct)
self.hex_oct.clicked.connect(self.hex__oct)
self.bin_dec.clicked.connect(self.bin__dec)
self.oct_dec.clicked.connect(self.oct__dec)
self.hex_dec.clicked.connect(self.hex__dec)
self.bin_hex.clicked.connect(self.bin__hex)
self.oct_hex.clicked.connect(self.oct__hex)
self.dec_hex.clicked.connect(self.dec__hex)
self.Empty.clicked.connect(self.empty)
def empty(self):
self.lineEdit.setText("")
def hex__bin(self):
s = self.lineEdit.text()
ss=int(str(s),16)
st=bin(ss).replace("0b","")
self.lineEdit_2.setText(str(st))
def bin__hex(self):
s = self.lineEdit.text()
ss=int(str(s),2)
st=hex(ss).replace('0x','')
self.lineEdit_2.setText(str(st))
def bin__oct(self):
s = self.lineEdit.text()
st = int(str(s), 2)
ss = oct(st).replace("0o", "")
self.lineEdit_2.setText(str(ss))
def bin__dec(self):
s = self.lineEdit.text()
ss = int(str(s), 2)
self.lineEdit_2.setText(str(ss))
def oct__bin(self):
s = self.lineEdit.text()
st = int(str(s), 8)
ss = bin(st).replace("0b", "")
self.lineEdit_2.setText(str(ss))
def oct__dec(self):
s = self.lineEdit.text()
ss = int(str(s), 8)
self.lineEdit_2.setText(str(ss))
def oct__hex(self):
s = self.lineEdit.text()
st = int(str(s), 8)
ss = hex(st).replace("0x", "")
self.lineEdit_2.setText(str(ss))
def dec__bin(self):
s = self.lineEdit.text()
ss = bin(int(s)).replace("0b", "")
self.lineEdit_2.setText(str(ss))
def dec__oct(self):
s = self.lineEdit.text()
ss = oct(int(s)).replace("0o", "")
self.lineEdit_2.setText(str(ss))
def dec__hex(self):
s = self.lineEdit.text()
ss = hex(int(s)).replace("0x", "")
self.lineEdit_2.setText(str(ss))
def hex__oct(self):
s = self.lineEdit.text()
st = int(str(s), 16)
ss = oct(st).replace("0o", "")
self.lineEdit_2.setText(str(ss))
def hex__dec(self):
s = self.lineEdit.text()
ss = int(str(s), 16)
self.lineEdit_2.setText(str(ss))
def tomain(self):
self.hide()
self.f = MainInterfaceUI()
self.f.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainInterfaceUI()
w.show()
sys.exit(app.exec_())
进制转换界面:
16进制和字符串转换界面:
代码很简单,我就不单独解释了,有问题随时私信我,欢迎交流讨论,完整的代码(包含UI文件)在我的gitee中:https://gitee.com/ximulalala/hex-str.git