1,允许转载,只需在该博客下的评论下评论,并附上自己的博客地址即可
2,不允许伪原创
3,其他暂议
在python的文件下找到script文件夹
打开文件夹Scripts
复制该文件下的路径
然后配置系统变量
点击高级系统设置
点击环境变量
选择Path点击编辑
然后点击新建,添加当时的路径——D:\写你自己的安装路径\Python37\Scripts
之后再一直点确定就可以了
之后就可以在命令行使用pip了
需要下载的库graphviz和pyqt5
graphviz安装地址
https://graphviz.gitlab.io/_pages/Download/windows/graphviz-2.38.msi
https://www2.graphviz.org/Packages/stable/windows/10/msbuild/Release/Win32/graphviz-2.38-win32.msi
然后命令行输入
pip install graphviz
pip install PyQt5
如果安装graphviz或PyQt5有Bug,请百度自行解决
创建一个目录
将main.py,interface.py,Huffman.py,demo.txt文件,放在同一个目录下
main.py——实现各个界面各个按钮的功能
from PyQt5 import QtCore, QtGui, QtWidgets
from interface import *
from Huffman import *
import sys,os,sip
import win32ui
class window1(QtWidgets.QWidget, Ui_MainWindow_main):
def __init__ (self):
super(window1, self).__init__()# 用父类的初始化方法初始化父类的各个属性(执行__init__()方法)
self.setupUi(self)
self.pushButton_main_haffman.clicked.connect(self.pushButton_1)
def pushButton_1(self):
try:
w2.show()
w1.close()
except:
print("pushButton_1:error")
class window2(QtWidgets.QWidget, Ui_MainWindow_select):
def __init__ (self):
super(window2, self).__init__()# 用父类的初始化方法初始化父类的各个属性(执行__init__()方法)
self.setupUi(self)
self.pushButton_select_1.clicked.connect(self.pushButton_1)
self.pushButton_select_2.clicked.connect(self.pushButton_2)
self.pushButton_select_3.clicked.connect(self.pushButton_3)
self.pushButton_select_return.clicked.connect(self.pushButton_return)
def pushButton_1(self):
try:
w2_1.f1()
w2_1.show()
w2.close()
except:
print("pushButton_1:error")
def pushButton_2(self):
try:
w2_2.f2()
w2_2.show()
w2.close()
except:
print("pushButton_2:error")
def pushButton_3(self):
try:
w2_3.f3()
w2_3.show()
w2.close()
except:
print("pushButton_3:error")
def pushButton_return(self):
try:
w1.show()
w2.close()
except:
print("pushButton_return:error")
class window2_1(QtWidgets.QWidget):
def __init__ (self):
super(window2_1, self).__init__()# 用父类的初始化方法初始化父类的各个属性(执行__init__()方法)
def f1(self):
self.m1=Ui_MainWindow_select_1()
self.m1.setupUi(self)
self.m1.function1_pushButton_2.clicked.connect(self.pushButton_1_2)
self.m1.function1_pushButton_3.clicked.connect(self.pushButton_1_3)
self.m1.function1_lineEdit_1.setText("a b c d e f")
self.m1.function1_lineEdit_2.setText("2 3 4 5 6 4")
def pushButton_1_2(self):
try:
string1=self.m1.function1_lineEdit_1.text()
string2 = self.m1.function1_lineEdit_2.text()
num1 = []
num2 = []
for i in string1.split(" "):
num1.append(i)
for i in string2.split(" "):
num2.append(int(i))
num3 = ""
for i in range(0, len(num1)):
for j in range(0, num2[i]):
num3 = num3 + num1[i]
string=num3
cs = createTree() # 创建一个实例
s = cs.Tree(string) # 调用该实例,构建一个树
k = haffman_code()
p = k.dict_encode(s)
m = k.dict_decode(string, p)
tu = haffman_png()
tu.hafuman_tree_tu(s)
self.m1.function1_textEdit_3.setText(m) # 将获取的哈夫曼编码写入名为“lineEdit_2”可编辑文本框中
string = os.getcwd() + r"\data.txt"
with open(string, 'w+') as f:
# f.write(str(p))
string1 = str(p).lstrip("{").rstrip("}")
s = ", "
for i in string1.split(s):
f.write(i)
f.write("\n")
success_result = r'成功储存在data.txt'
self.m1.function1_label_4.setText(success_result)
except:
fail_result = r'失败!'
self.m1.function1_label_4.setText(fail_result)
def pushButton_1_3(self):
try:
w2.show()
w2_1.close()
except:
pass
class window2_2(QtWidgets.QWidget):
def __init__(self):
super(window2_2, self).__init__() # 用父类的初始化方法初始化父类的各个属性(执行__init__()方法)
def f2(self):
self.m2 = Ui_MainWindow_select_2()
self.m2.setupUi(self)
self.m2.function2_pushButton_2.clicked.connect(self.pushButton_2_2)
self.m2.function2_pushButton_3.clicked.connect(self.pushButton_2_3)
self.m2.function2_pushButton_4.clicked.connect(self.pushButton_2_4)
string = "AAGGDCCCDDDGFBBBFFGGDDDDGGGEFFDDCCCCDDFGAAA"
self.m2.function2_lineEdit_input.setText(string) # 将示例字符写入可编辑文本框中
def pushButton_2_2(self):
try:
string = self.m2.function2_lineEdit_input.text()
cs=""
cs = createTree() # 创建一个实例
s = cs.Tree(string) # 调用该实例,构建一个树
k = haffman_code()
p = k.dict_encode(s)
m = k.dict_decode(string, p)
self.m2.function2_textEdit_output_1.setText(str(p))
self.m2.function2_textEdit_output_2.setText(m) # 将获取的哈夫曼编码写入名为“lineEdit_2”可编辑文本框中
string = os.getcwd()+r"\data.txt"
with open(string, 'w+') as f:
# f.write(str(p))
string1 = str(p).lstrip("{").rstrip("}")
s = ", "
for i in string1.split(s):
f.write(i)
f.write("\n")
success_result = r'成功储存在data.txt'
self.m2.function2_label_4.setText(success_result)
except:
fail_result = r'失败!'
self.m2.function2_label_4.setText(fail_result)
def pushButton_2_3(self):
try:
string = self.m2.function2_lineEdit_input.text()
cs = createTree() # 创建一个实例
s = cs.Tree(string) # 调用该实例,构建一个树
tu = haffman_png()
tu.hafuman_tree_tu(s)
except:
print("pushButton_2_3:error")
def pushButton_2_4(self):
try:
# self.close()
w2.show()
w2_2.close()
except:
pass
class window2_3(QtWidgets.QWidget):
def __init__(self):
super(window2_3, self).__init__() # 用父类的初始化方法初始化父类的各个属性(执行__init__()方法)
def f3(self):
self.m3=Ui_MainWindow_select_3()
self.m3.setupUi(self)
self.m3.function3_pushButton_2.clicked.connect(self.pushButton_3_2)
self.m3.function3_pushButton_3.clicked.connect(self.pushButton_3_3)
self.m3.function3_pushButton_4.clicked.connect(self.pushButton_3_4)
self.m3.function3_pushButton_5.clicked.connect(self.pushButton_3_5)
self.m3.function3_pushButton_6.clicked.connect(self.pushButton_3_6)
string = os.getcwd()+r"\demo.txt"
string1 =os.getcwd()+ r"\data.txt"
self.m3.function3_lineEdit_1.setText(string) # 将示例字符写入可编辑文本框中
self.m3.function3_lineEdit_2.setText(string1) # 将示例字符写入可编辑文本框中
def pushButton_3_2(self):
try:
dlg = win32ui.CreateFileDialog(1) # 1表示打开文件对话框
dlg.SetOFNInitialDir('c:/') # 设置打开文件对话框中的初始显示目录
dlg.DoModal()
filename = dlg.GetPathName() # 获取选择的文件名称
if(filename==""):
filename = self.m3.function3_lineEdit_1.text()
filepath = filename.split(".")[0]
filepath = filepath + "\\"+"data.txt"
self.m3.function3_lineEdit_1.setText(filename) # 将获取的文件名称写入名为“lineEdit_1”可编辑文本框中
self.m3.function3_lineEdit_2.setText(filepath) # 将获取的文件名称写入名为“lineEdit_2”可编辑文本框中
# self.m3.function3_textBrowser_1.setText(filepath)
except:
print("pushButton_3_2:error")
def pushButton_3_3(self):
try:
dlg = win32ui.CreateFileDialog(1) # 1表示打开文件对话框
dlg.SetOFNInitialDir('c:/') # 设置打开文件对话框中的初始显示目录
dlg.DoModal()
filename = dlg.GetPathName() # 获取选择的文件名称
if (filename == ""):
filename = self.m3.function3_lineEdit_2.text()
self.m3.function3_lineEdit_2.setText(filename)
except:
print("pushButton_3_3:error")
def pushButton_3_4(self):
try:
# self.m3.function3_lineEdit_1.setText(string) # 将示例字符写入可编辑文本框中
file = self.m3.function3_lineEdit_1.text()
with open(file,'r') as f:
string=f.read()
cs = createTree() # 创建一个实例
s = cs.Tree(string) # 调用该实例,构建一个树
k = haffman_code()
p = k.dict_encode(s)
m = k.dict_decode(string, p)
self.m3.function3_textBrowser_1.setText(string)
self.m3.function3_textBrowser_2.setText(m) # 将获取的哈夫曼编码写入可编辑文本框中
string=self.m3.function3_lineEdit_2.text()
with open(string,'w+') as f:
# f.write(str(p))
string1 = str(p).lstrip("{").rstrip("}")
s = ", "
for i in string1.split(s):
f.write(i)
f.write("\n")
except:
print("pushButton_3_4:error")
def pushButton_3_5(self):
try:
file = self.m3.function3_lineEdit_1.text()
with open(file, 'r') as f:
string = f.read()
cs = createTree() # 创建一个实例
s = cs.Tree(string) # 调用该实例,构建一个树
tu = haffman_png()
tu.hafuman_tree_tu(s)
except:
print("pushButton_3_5:error")
def pushButton_3_6(self):
try:
w2.show()
w2_3.close()
except:
print("pushButton_3_6:error")
if __name__=="__main__":
app = QtWidgets.QApplication(sys.argv)
w1 = window1()
w2 = window2()
w2_1 = window2_1()
w2_2 = window2_2()
w2_3 = window2_3()
w1.show()
sys.exit(app.exec_())
#########################
##############
################
#########
interface.py——实现一个空界面(光有界面,没有相应的功能)
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'window1.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# 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_MainWindow_main(object):
def setupUi(self, MainWindow_main):
MainWindow_main.setObjectName("MainWindow_main")
MainWindow_main.resize(831, 381)
MainWindow_main.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
self.centralwidget = QtWidgets.QWidget(MainWindow_main)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_main_haffman = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_main_haffman.setGeometry(QtCore.QRect(220, 70, 371, 211))
font = QtGui.QFont()
font.setPointSize(21)
self.pushButton_main_haffman.setFont(font)
self.pushButton_main_haffman.setIconSize(QtCore.QSize(211, 373))
self.pushButton_main_haffman.setCheckable(False)
self.pushButton_main_haffman.setChecked(False)
self.pushButton_main_haffman.setObjectName("pushButton_main_haffman")
self.pushButton_main_jishao = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_main_jishao.setGeometry(QtCore.QRect(670, 30, 93, 28))
font = QtGui.QFont()
font.setBold(False)
font.setWeight(50)
self.pushButton_main_jishao.setFont(font)
self.pushButton_main_jishao.setObjectName("pushButton_main_jishao")
self.pushButton_end = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_end.setGeometry(QtCore.QRect(680, 270, 93, 28))
self.pushButton_end.setObjectName("pushButton_end")
#MainWindow_main.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow_main)
self.menubar.setGeometry(QtCore.QRect(0, 0, 831, 26))
self.menubar.setObjectName("menubar")
#MainWindow_main.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow_main)
self.statusbar.setObjectName("statusbar")
#MainWindow_main.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow_main)
self.pushButton_end.clicked.connect(MainWindow_main.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow_main)
def retranslateUi(self, MainWindow_main):
_translate = QtCore.QCoreApplication.translate
MainWindow_main.setWindowTitle(_translate("MainWindow_main", "主界面"))
self.pushButton_main_haffman.setText(_translate("MainWindow_main", "哈夫曼"))
self.pushButton_main_jishao.setText(_translate("MainWindow_main", "使用说明"))
self.pushButton_end.setText(_translate("MainWindow_main", "退出程序"))
def retranslateUi(self, MainWindow_main):
_translate = QtCore.QCoreApplication.translate
MainWindow_main.setWindowTitle(_translate("MainWindow_main", "主界面"))
self.pushButton_main_haffman.setText(_translate("MainWindow_main", "哈夫曼"))
self.pushButton_main_jishao.setText(_translate("MainWindow_main", "使用说明"))
self.pushButton_end.setText(_translate("MainWindow_main", "退出程序"))
class Ui_MainWindow_select(object):
def setupUi(self, MainWindow_select):
MainWindow_select.setObjectName("MainWindow_select")
MainWindow_select.resize(831, 381)
self.centralwidget = QtWidgets.QWidget(MainWindow_select)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_select_1 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_select_1.setGeometry(QtCore.QRect(30, 110, 191, 111))
self.pushButton_select_1.setIconSize(QtCore.QSize(211, 373))
self.pushButton_select_1.setCheckable(False)
self.pushButton_select_1.setChecked(False)
self.pushButton_select_1.setObjectName("pushButton_select_1")
self.pushButton_select_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_select_2.setGeometry(QtCore.QRect(290, 110, 191, 111))
self.pushButton_select_2.setIconSize(QtCore.QSize(211, 373))
self.pushButton_select_2.setCheckable(False)
self.pushButton_select_2.setChecked(False)
self.pushButton_select_2.setObjectName("pushButton_select_2")
self.pushButton_select_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_select_3.setGeometry(QtCore.QRect(560, 110, 191, 111))
self.pushButton_select_3.setIconSize(QtCore.QSize(211, 373))
self.pushButton_select_3.setCheckable(False)
self.pushButton_select_3.setChecked(False)
self.pushButton_select_3.setObjectName("pushButton_select_3")
self.pushButton_select_jieshao = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_select_jieshao.setGeometry(QtCore.QRect(720, 30, 93, 28))
self.pushButton_select_jieshao.setObjectName("pushButton_select_jieshao")
self.pushButton_select_return = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_select_return.setGeometry(QtCore.QRect(570, 270, 171, 51))
self.pushButton_select_return.setObjectName("pushButton_select_return")
self.label_1 = QtWidgets.QLabel(self.centralwidget)
self.label_1.setGeometry(QtCore.QRect(90, 60, 91, 31))
self.label_1.setObjectName("label_1")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(360, 60, 91, 31))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(630, 70, 91, 31))
self.label_3.setObjectName("label_3")
#MainWindow_select.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow_select)
self.menubar.setGeometry(QtCore.QRect(0, 0, 831, 26))
self.menubar.setObjectName("menubar")
#MainWindow_select.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow_select)
self.statusbar.setObjectName("statusbar")
#MainWindow_select.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow_select)
QtCore.QMetaObject.connectSlotsByName(MainWindow_select)
def retranslateUi(self, MainWindow_select):
_translate = QtCore.QCoreApplication.translate
MainWindow_select.setWindowTitle(_translate("MainWindow_select", "功能选择"))
self.pushButton_select_1.setText(_translate("MainWindow_select", "输入字符和权值\n"
"得到哈夫曼编码"))
self.pushButton_select_2.setText(_translate("MainWindow_select", "输入字符串得到哈夫曼编码"))
self.pushButton_select_3.setText(_translate("MainWindow_select", "输入文件读取其中字符,\n"
"得到哈夫曼编码"))
self.pushButton_select_jieshao.setText(_translate("MainWindow_select", "使用说明"))
self.pushButton_select_return.setText(_translate("MainWindow_select", "返回主界面"))
self.label_1.setText(_translate("MainWindow_select", "功能1"))
self.label_2.setText(_translate("MainWindow_select", "功能2"))
self.label_3.setText(_translate("MainWindow_select", "功能3"))
class Ui_MainWindow_select_1(object):
def setupUi(self, MainWindow_select_1):
MainWindow_select_1.setObjectName("MainWindow_select_1")
MainWindow_select_1.resize(831, 381)
self.centralwidget = QtWidgets.QWidget(MainWindow_select_1)
self.centralwidget.setObjectName("centralwidget")
self.function1_label_1 = QtWidgets.QLabel(self.centralwidget)
self.function1_label_1.setGeometry(QtCore.QRect(40, 60, 181, 16))
self.function1_label_1.setObjectName("function1_label_1")
self.function1_label_2 = QtWidgets.QLabel(self.centralwidget)
self.function1_label_2.setGeometry(QtCore.QRect(240, 60, 251, 16))
self.function1_label_2.setObjectName("function1_label_2")
self.function1_label_3 = QtWidgets.QLabel(self.centralwidget)
self.function1_label_3.setGeometry(QtCore.QRect(30, 260, 71, 16))
self.function1_label_3.setObjectName("function1_label_3")
self.function1_textEdit_3 = QtWidgets.QTextEdit(self.centralwidget)
self.function1_textEdit_3.setGeometry(QtCore.QRect(110, 240, 251, 61))
self.function1_textEdit_3.setObjectName("function1_textEdit_3")
self.function1_pushButton_1 = QtWidgets.QPushButton(self.centralwidget)
self.function1_pushButton_1.setGeometry(QtCore.QRect(650, 50, 93, 28))
self.function1_pushButton_1.setObjectName("function1_pushButton_1")
self.function1_pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.function1_pushButton_2.setGeometry(QtCore.QRect(520, 110, 93, 28))
self.function1_pushButton_2.setObjectName("function1_pushButton_2")
self.function1_label_4 = QtWidgets.QLabel(self.centralwidget)
self.function1_label_4.setGeometry(QtCore.QRect(470, 260, 191, 31))
self.function1_label_4.setText("")
self.function1_label_4.setObjectName("function1_label_4")
self.function1_pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.function1_pushButton_3.setGeometry(QtCore.QRect(600, 280, 141, 28))
self.function1_pushButton_3.setObjectName("function1_pushButton_3")
self.function1_lineEdit_1 = QtWidgets.QLineEdit(self.centralwidget)
self.function1_lineEdit_1.setGeometry(QtCore.QRect(30, 103, 201, 81))
self.function1_lineEdit_1.setObjectName("function1_lineEdit_1")
self.function1_lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.function1_lineEdit_2.setGeometry(QtCore.QRect(250, 100, 201, 81))
self.function1_lineEdit_2.setObjectName("function1_lineEdit_2")
#MainWindow_select_1.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow_select_1)
self.menubar.setGeometry(QtCore.QRect(0, 0, 831, 26))
self.menubar.setObjectName("menubar")
#ainWindow_select_1.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow_select_1)
self.statusbar.setObjectName("statusbar")
#MainWindow_select_1.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow_select_1)
QtCore.QMetaObject.connectSlotsByName(MainWindow_select_1)
def retranslateUi(self, MainWindow_select_1):
_translate = QtCore.QCoreApplication.translate
MainWindow_select_1.setWindowTitle(_translate("MainWindow_select_1", "功能1"))
self.function1_label_1.setText(_translate("MainWindow_select_1", "输入字符,用空格隔开"))
self.function1_label_2.setText(_translate("MainWindow_select_1", "输入字符所对应的权值,用空格隔开"))
self.function1_label_3.setText(_translate("MainWindow_select_1", "编码结果"))
self.function1_pushButton_1.setText(_translate("MainWindow_select_1", "使用说明"))
self.function1_pushButton_2.setText(_translate("MainWindow_select_1", "开始转化"))
self.function1_pushButton_3.setText(_translate("MainWindow_select_1", "返回上一个界面"))
class Ui_MainWindow_select_2(object):
def setupUi(self, MainWindow_select_2):
MainWindow_select_2.setObjectName("MainWindow_select_2")
MainWindow_select_2.resize(831, 381)
self.centralwidget = QtWidgets.QWidget(MainWindow_select_2)
self.centralwidget.setObjectName("centralwidget")
self.function2_frame = QtWidgets.QFrame(self.centralwidget)
self.function2_frame.setGeometry(QtCore.QRect(10, 20, 811, 381))
self.function2_frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.function2_frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.function2_frame.setObjectName("function2_frame")
self.function2_pushButton_2 = QtWidgets.QPushButton(self.function2_frame)
self.function2_pushButton_2.setGeometry(QtCore.QRect(290, 290, 93, 28))
self.function2_pushButton_2.setObjectName("function2_pushButton_2")
self.function2_lineEdit_input = QtWidgets.QLineEdit(self.function2_frame)
self.function2_lineEdit_input.setGeometry(QtCore.QRect(250, 52, 481, 24))
self.function2_lineEdit_input.setObjectName("function2_lineEdit_input")
self.function2_label_1 = QtWidgets.QLabel(self.function2_frame)
self.function2_label_1.setGeometry(QtCore.QRect(60, 50, 135, 28))
self.function2_label_1.setObjectName("function2_label_1")
self.function2_pushButton_4 = QtWidgets.QPushButton(self.function2_frame)
self.function2_pushButton_4.setGeometry(QtCore.QRect(670, 290, 131, 28))
self.function2_pushButton_4.setObjectName("function2_pushButton_4")
self.function2_label_2 = QtWidgets.QLabel(self.function2_frame)
self.function2_label_2.setGeometry(QtCore.QRect(50, 120, 151, 21))
self.function2_label_2.setObjectName("function2_label_2")
self.function2_label_3 = QtWidgets.QLabel(self.function2_frame)
self.function2_label_3.setGeometry(QtCore.QRect(50, 210, 135, 28))
self.function2_label_3.setObjectName("function2_label_3")
self.function2_textEdit_output_1 = QtWidgets.QTextEdit(self.function2_frame)
self.function2_textEdit_output_1.setGeometry(QtCore.QRect(250, 100, 481, 61))
self.function2_textEdit_output_1.setObjectName("function2_textEdit_output_1")
self.function2_textEdit_output_2 = QtWidgets.QTextEdit(self.function2_frame)
self.function2_textEdit_output_2.setGeometry(QtCore.QRect(250, 190, 481, 61))
self.function2_textEdit_output_2.setObjectName("function2_textEdit_output_2")
self.function2_pushButton_1 = QtWidgets.QPushButton(self.function2_frame)
self.function2_pushButton_1.setGeometry(QtCore.QRect(720, 10, 93, 28))
self.function2_pushButton_1.setObjectName("function2_pushButton_1")
self.function2_pushButton_3 = QtWidgets.QPushButton(self.function2_frame)
self.function2_pushButton_3.setGeometry(QtCore.QRect(460, 290, 181, 28))
self.function2_pushButton_3.setObjectName("function2_pushButton_3")
self.function2_label_4 = QtWidgets.QLabel(self.function2_frame)
self.function2_label_4.setGeometry(QtCore.QRect(40, 280, 135, 28))
self.function2_label_4.setText("")
self.function2_label_4.setObjectName("function2_label_4")
#MainWindow_select_2.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow_select_2)
self.menubar.setGeometry(QtCore.QRect(0, 0, 831, 26))
self.menubar.setObjectName("menubar")
#MainWindow_select_2.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow_select_2)
self.statusbar.setObjectName("statusbar")
#MainWindow_select_2.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow_select_2)
QtCore.QMetaObject.connectSlotsByName(MainWindow_select_2)
def retranslateUi(self, MainWindow_select_2):
_translate = QtCore.QCoreApplication.translate
MainWindow_select_2.setWindowTitle(_translate("MainWindow_select_2", "功能2"))
self.function2_pushButton_2.setText(_translate("MainWindow_select_2", "开始转化"))
self.function2_label_1.setText(_translate("MainWindow_select_2", "请输入字符"))
self.function2_pushButton_4.setText(_translate("MainWindow_select_2", "返回上一个界面"))
self.function2_label_2.setText(_translate("MainWindow_select_2", "随对应的哈夫曼编码表"))
self.function2_label_3.setText(_translate("MainWindow_select_2", "得到该哈夫曼编码"))
self.function2_pushButton_1.setText(_translate("MainWindow_select_2", "使用说明"))
self.function2_pushButton_3.setText(_translate("MainWindow_select_2", "显示该字符串的二叉树"))
class Ui_MainWindow_select_3(object):
def setupUi(self, MainWindow_select_3):
MainWindow_select_3.setObjectName("MainWindow_select_3")
MainWindow_select_3.resize(831, 381)
self.centralwidget = QtWidgets.QWidget(MainWindow_select_3)
self.centralwidget.setObjectName("centralwidget")
self.function3_textBrowser_2 = QtWidgets.QTextBrowser(self.centralwidget)
self.function3_textBrowser_2.setGeometry(QtCore.QRect(280, 190, 211, 151))
self.function3_textBrowser_2.setObjectName("function3_textBrowser_2")
self.function3_textBrowser_1 = QtWidgets.QTextBrowser(self.centralwidget)
self.function3_textBrowser_1.setGeometry(QtCore.QRect(20, 190, 211, 151))
self.function3_textBrowser_1.setObjectName("function3_textBrowser_1")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(310, 160, 161, 31))
self.label_4.setObjectName("label_4")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(26, 100, 121, 16))
self.label_2.setObjectName("label_2")
self.function3_pushButton_1 = QtWidgets.QPushButton(self.centralwidget)
self.function3_pushButton_1.setGeometry(QtCore.QRect(730, 30, 93, 28))
self.function3_pushButton_1.setObjectName("function3_pushButton_1")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(28, 58, 135, 16))
self.label.setObjectName("label")
self.function3_pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.function3_pushButton_2.setGeometry(QtCore.QRect(632, 58, 93, 28))
self.function3_pushButton_2.setObjectName("function3_pushButton_2")
self.function3_lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.function3_lineEdit_2.setGeometry(QtCore.QRect(168, 102, 371, 24))
self.function3_lineEdit_2.setObjectName("function3_lineEdit_2")
self.function3_pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.function3_pushButton_4.setGeometry(QtCore.QRect(610, 210, 141, 28))
self.function3_pushButton_4.setObjectName("function3_pushButton_4")
self.function3_lineEdit_1 = QtWidgets.QLineEdit(self.centralwidget)
self.function3_lineEdit_1.setGeometry(QtCore.QRect(170, 60, 371, 24))
self.function3_lineEdit_1.setObjectName("function3_lineEdit_1")
self.function3_pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
self.function3_pushButton_6.setGeometry(QtCore.QRect(610, 310, 141, 28))
self.function3_pushButton_6.setObjectName("function3_pushButton_6")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(50, 160, 141, 31))
self.label_3.setObjectName("label_3")
self.function3_pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.function3_pushButton_5.setGeometry(QtCore.QRect(610, 260, 141, 28))
self.function3_pushButton_5.setObjectName("function3_pushButton_5")
self.function3_pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.function3_pushButton_3.setGeometry(QtCore.QRect(630, 100, 93, 28))
self.function3_pushButton_3.setObjectName("function3_pushButton_3")
#MainWindow_select_3.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow_select_3)
self.menubar.setGeometry(QtCore.QRect(0, 0, 831, 26))
self.menubar.setObjectName("menubar")
#MainWindow_select_3.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow_select_3)
self.statusbar.setObjectName("statusbar")
#MainWindow_select_3.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow_select_3)
QtCore.QMetaObject.connectSlotsByName(MainWindow_select_3)
def retranslateUi(self, MainWindow_select_3):
_translate = QtCore.QCoreApplication.translate
MainWindow_select_3.setWindowTitle(_translate("MainWindow_select_3", "功能3"))
self.label_4.setText(_translate("MainWindow_select_3", "所读取文件转化的编码"))
self.label_2.setText(_translate("MainWindow_select_3", "选择输出的文件"))
self.function3_pushButton_1.setText(_translate("MainWindow_select_3", "使用说明"))
self.label.setText(_translate("MainWindow_select_3", "请输入文件所在路径
"))
self.function3_pushButton_2.setText(_translate("MainWindow_select_3", "添加文件"))
self.function3_pushButton_4.setText(_translate("MainWindow_select_3", "开始转化"))
self.function3_pushButton_6.setText(_translate("MainWindow_select_3", "返回上一个界面"))
self.label_3.setText(_translate("MainWindow_select_3", "所读取文件中的字符"))
self.function3_pushButton_5.setText(_translate("MainWindow_select_3", "显示二叉树"))
self.function3_pushButton_3.setText(_translate("MainWindow_select_3", "添加文件夹"))
Huffman.py——哈夫曼树算法(构造树,生成图片等)
import graphviz
import sys
class TreeNode(object):
def __init__(self, data):
self.data =data #权值
self.code = "" #所对应的字符
self.char = "" #对于根节点来说,是左(0)是右(1)
self.str = "" #该字符所对应的编码
# self.sign="" #防止同一根节点下的左右节点的权值一样,加这一个变量是为了画哈夫曼树
self.lchild = None
self.rchild = None
class createTree():
def __init__(self):
pass
def sort(self,k): # 输入一串字符,返回一个排好序的列表的列表,如:输入k="fsdafdfa",返回[['s', 1], ['d', 2], ['a', 2], ['f', 3]]
list1 = []
list2 = []
list3 = []
for i in k:
char = i
if (char not in list1):
list1.append(char)
for i in list1:
dict1 = ["", 0]
dict1[0] = i
list2.append(dict1)
for i in k:
for j in range(0, len(list1)):
if (i == list1[j]):
list2[j][1] = list2[j][1] + 1
count = len(list2)
for i in range(0, count): # 比较list2[x][1]值的大小,给它进行冒泡排序
num = list2[i][1]
p = i
for j in range(i, count): # 遍历list2中的权值,找出其中的最小值,并记录他在列表中的位置
if (num > list2[j][1]):
num = list2[j][1]
p = j
if (j == count - 1): # 冒泡排序中的换位代码
qw1 = list2[p]
list2[p] = list2[i]
list2[i] = qw1
list3.append(qw1) # 在list3中添加最下的权值
return list3
def createnodeQ(self, codes): # 主要是,创造叶子节点,然后被下一个函数Tree()调用
self.q = []
i=0
codes = self.sort(codes) #调用sort()方法,输入一串字符,返回一个排好序的列表的列表,如:输入k="fsdafdfa",
# 返回[['s', 1], ['d', 2], ['a', 2], ['f', 3]]
for code in codes:
self.q.append(TreeNode(code[1])) # 构建出内容全是节点对象的列表,利用权值构建该列表
self.q[i].code = code[0]
i=i+1
return self.q
def Tree(self, codes): #输入权值序列,升序排序输入一个列表,如:k=[1,2,3,4,5,6,7,8,9]
self.k = self.createnodeQ(codes)
m=0
i = 0
j = len(self.k)
#构建哈夫曼树的主要算法如下:
#因为已经排好序,所以k[i].data ,k[i + 1].data是列表k中最小的值
#s=self.k[i].data + self.k[i + 1].data
#所以s就是最小的两个权值之和
#算法主要是构建一个self.k.insert(q,TreeNode(s))
#找到合适的地方插入列表k
#然后再出栈,剔除列表中k[x].data两个最小的值
#重复这些过程,知道len(k)=1后跳出循环
while (j != 1):
q=0
for q in range(0, len(self.k)):
s=self.k[i].data + self.k[i + 1].data
if (s <= self.k[q].data):
q=q
self.k.insert(q,TreeNode(s))
self.tree = []
self.k[i].char="0"
self.tree.append(self.k[i])
self.k[q].lchild=self.tree
self.tree = []
self.k[i+1].char = "1"
self.tree.append(self.k[i+1])
self.k[q].rchild = self.tree
# p = tree[0].data
#print(q,self.k[q].data,self.k[i].data,self.k[i+1].data)
break
if (s > self.k[j-1].data):
s = self.k[i].data + self.k[i + 1].data
q=j
self.k.append(TreeNode(s))
self.tree = []
self.k[i].char = "0"
self.tree.append(self.k[i])
self.k[q].lchild = self.tree
self.tree = []
self.k[i+1].char = "1"
self.tree.append(self.k[i + 1])
self.k[q].rchild = self.tree
#print(q, self.k[q].data, self.k[i].data, self.k[i + 1].data,"q")
# print(self.k[q].data,self.k[q].lchild[0].data,self.k[q].rchild[0].data)
break
self.k.pop(0)
self.k.pop(0)
j = len(self.k)
if(j==1):
return self.k
#haffuman_tree(k):将哈夫曼树可视化
# 利用先序遍历算法,和栈的知识,把根结点发放在栈中,根结点
#的级别越高,越接近栈底,如父结点就放在栈底node[0].每遍历一个节点后,将其放入栈中,
# 并创造相应的节点如:node.append(str(k[0].data))
# g.node(name=node[len(node)-1], color='red')
# 当每遍历到叶节点时
#就将其放到栈中,此时利用函数连接栈的,后两位数据(也就是加在函数最后的代码的字符,
# if (len(node) >= 2):
# g.edge(node[len(node) - 2], node[len(node) - 1], color='green')
# print(node[len(node) - 2], node[len(node) - 1])
# node.pop()
#然后出栈
#循环这个过程就可以实现哈夫曼树可视化了
class haffman_png():
def __init__(self):
self.g = graphviz.Digraph(filename="cs", format='png') # 被hafuman_tree()函数调用
self.node = [] # 这三行代码必须合在一起
self.tree = [] #
self.sign=[] #记录所创造的节点
def hafuman_tree_tu(self,k): # 才可画出哈夫曼树
if (k != []):
# print(k[0].data)
self.tree.append(k[0])
if (k[0].code != ""):
self.node.append(k[0].code + "-" + str(k[0].data))
else:
self.node.append(str(k[0].data))
self.tree[len(self.tree) - 1].str = self.tree[len(self.tree) - 2].str + self.tree[len(self.tree) - 1].char
#print(self.tree[len(self.tree) - 1].str, self.tree[len(self.tree) - 1].code)
p=self.node[len(self.node) - 1]
while(p in self.sign):
p=p+" "
self.node[len(self.node) - 1]=p
self.g.node(name=self.node[len(self.node) - 1], color='red')
self.sign.append(self.node[len(self.node) - 1])
try:
self.hafuman_tree_tu(k[0].lchild)
except:
pass
try:
self.hafuman_tree_tu(k[0].rchild)
except:
pass
if (len(self.node) >= 2):
# print(tree[len(tree)-1].char,"f")
self.g.edge(self.node[len(self.node) - 2], self.node[len(self.node) - 1],self.tree[len(self.tree) - 1].char, color='green')
# g.view() #去掉注释有惊喜
# print(node[len(node) - 2], node[len(node) - 1])
self.node.pop()
self.tree.pop()
# g.view() #去掉注释有惊喜
if (len(self.node) == 0):
self.g.view()
class haffman_code():
def __init__(self):
self.dict1={
}
self.tree=[]
def dict_encode(self,k): #输入哈夫曼树,返回哈夫曼编码字典
if (k != []):
# print(k[0].data)
self.tree.append(k[0])
self.tree[len(self.tree) - 1].str = self.tree[len(self.tree) - 2].str+self.tree[len(self.tree) - 1].char
if(self.tree[len(self.tree) - 1].code!=""):
self.dict1[self.tree[len(self.tree) - 1].code]=self.tree[len(self.tree) - 1].str
try:
self.dict_encode(k[0].lchild)
except:
pass
try:
self.dict_encode(k[0].rchild)
except:
pass
self.tree.pop()
return self.dict1
def dict_decode(self,str,dict1): #输入要解码的字符,和解码字典
list1=[]
self.decode_result=""
for i in str:
self.decode_result=self.decode_result+dict1[i]
return self.decode_result
# if __name__ == '__main__':
# string="aqqqwwwwweeeeeee"
# cs = createTree() #创建一个实例
# s=cs.Tree(string) #调用该实例,构建一个树
# tu=haffman_png()
# tu.hafuman_tree_tu(s)
#
# k=haffman_code()
# p=k.dict_encode(s)
# m=k.dict_decode(string,p)
# print(p)
# print(m)
#
#
demo.txt——一个示例文件
I'm LiHua , a Chinese student taking summer course in your university . I'm
writing to ask for help . I came here last month and found my courses
interesting .But I have some difficulty with note-taking and I have no idea of
how to use the library . I was told the learning center provides help for
students and I'm anxious to get help from you.