PyQt实战-实现hex-str转换和进制转换

博主分享了一段代码,用于创建一个简单的图形用户界面(GUI),该界面包含三个功能:进制转换、16进制与字符串互转。用户可以方便地在不同进制间转换数值,以及在16进制和字符串形式之间切换,无需频繁访问在线转换工具。代码基于PyQt5库,包括主界面、进制转换界面和16进制字符串转换界面,各功能按钮点击后触发相应转换操作。完整代码可在作者的Gitee仓库中找到。
摘要由CSDN通过智能技术生成

工作需要,频繁进行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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值