PyQt5实现简易电子词典


Posted in Python onJune 25, 2019

PyQt5是python中一个非常实用的GUI编程模块,功能十分强大。刚刚学完了Pyqt的编程,就迫不及待的写出了一个电子词典GUI程序。整个程序使用qt Desiner把整个gui界面做好,槽函数则自己写好的。电子词典实现了查询单词,查询历史记录,收藏和查看单词本的功能,另外为了是程序更加炫酷,还添加了一个启动界面。具体代码如下:

第一个为主程序代码,主要实现槽函数功能。

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow
from PyQt5 import QtWidgets
from Ui_E_Dict_Main import Ui_E_Dictory
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import time, re
 
# 槽函数代码,运行程序需要运行本文件
class MainWindow(QMainWindow, Ui_E_Dictory):
 """
 Class documentation goes here.
 """
 def __init__(self, parent=None):
  """
  Constructor
  
  @param parent reference to the parent widget
  @type QWidget
  """
  super(MainWindow, self).__init__(parent)
  self.setupUi(self)
  # 启动时休眠1秒
  time.sleep(1)
 
 # 按钮1 查找单词,把单词显示在textBrowser的同时,插入历史记录
 @pyqtSlot()
 def on_pushButton_clicked(self):
  """
  Slot documentation goes here.
  """
  # 单词查询,需要先有一个'dict.txt'文件,其中有大量的英文单词和注释
  # 此处也可以先把'dict.txt'插入数据库,历史记录和单词本的插入和查询都可以直接操作数据库
  # 不过数据库需要事先安装数据库,并建立相应的表,不好打包,不太方便
  word=self.lineEdit.text()
  f=open('dict.txt', 'r')
  for line in f:
   # 对字典文件的数据进行分析,拆解为适合显示的格式
   l = re.split('[ ]+',line)
   if l[0]==word:
    interpret=' '.join(l[1:])
    data='%s\n %s'%(l[0], interpret)
    # interpret='%s: %s'%(l[0],' '.join(l[1:]))
    self.textBrowser.setText(data)
    # 当地时间
    t1=time.localtime()
    t2=time.asctime(t1)
    #self.lineEdit.setText("")#lineEdit输入后清零,可要可不要
    try:
     # 把所查询单词插入历史记录中,
     #'a'以只写文件打开一个文件,如果有原文件则追加到文件末尾
     file=open('history.txt', 'at')
     msg='%s %s'%(word, t2)
     file.write(msg)
     file.write('\n')
     file.close()
    except Exception as e:
     print(e)
  f.close()
   
    
 @pyqtSlot()
 def on_pushButton_2_clicked(self):
  """
  Slot documentation goes here.
  """
  try:
   # 查询历史记录,把历史记录显示在textBrowser中
   file=open('history.txt', 'rt')   
   list=file.readlines()
   msg=''.join(list)
   self.textBrowser.setText(msg)
   file.close()
  except Exception as e:
     print(e)
     
  
 @pyqtSlot()
 def on_pushButton_3_clicked(self):
  """
  Slot documentation goes here.
  """
  try:
   # 查询单词本,把单词本显示在textBrowser中
   file=open('words.txt', 'rt')   
   list=file.readlines()
   msg=''.join(list)
   self.textBrowser.setText(msg)
   file.close()
  except Exception as e:
     print(e)
  
 
 
 @pyqtSlot()
 def on_pushButton_4_clicked(self):
  """
  Slot documentation goes here.
  """
  word=self.lineEdit.text()
  try:
   # 把所查询单词插入单词本中
   file=open('words.txt', 'at')
   file.write(word)
   file.write('\n')
   file.close()
  except Exception as e:
   print(e)  
 
  
  
if __name__ == "__main__":
 import sys
 app = QtWidgets.QApplication(sys.argv)
 # 启动界面的实现,可以使程序更加炫酷
 splash=QtWidgets.QSplashScreen(QPixmap("Saved Pictures/5b517f520feaa.jpg"))
 # 启动界面显示
 splash.show()
 # 在启动界面中显示程序加载进度,参数意思分别为居中显示,蓝色字体
 splash.showMessage('正在加载图片资源', Qt.AlignCenter, Qt.blue)
 time.sleep(1)
 # 为了不与主程序干扰
 app.processEvents()
 ui = MainWindow()
 ui.show()
 # 启动界面完成
 splash.finish(ui)
 sys.exit(app.exec_())

第二个程序代码,主要实现整体的GUI界面的构建(使用Qtdesiner可以极大的简化工作量,强烈推荐)

from PyQt5 import QtCore, QtGui, QtWidgets
 
class Ui_E_Dictory(object):
 def setupUi(self, E_Dictory):
  E_Dictory.setObjectName("E_Dictory")
  E_Dictory.resize(658, 474)
  icon = QtGui.QIcon()
  icon.addPixmap(QtGui.QPixmap("icon/24-monitor.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
  E_Dictory.setWindowIcon(icon)
  self.centralWidget = QtWidgets.QWidget(E_Dictory)
  self.centralWidget.setObjectName("centralWidget")
  self.pushButton = QtWidgets.QPushButton(self.centralWidget)
  self.pushButton.setGeometry(QtCore.QRect(390, 400, 91, 31))
  font = QtGui.QFont()
  font.setFamily("黑体")
  font.setPointSize(14)
  self.pushButton.setFont(font)
  self.pushButton.setObjectName("pushButton")
  self.label_2 = QtWidgets.QLabel(self.centralWidget)
  self.label_2.setGeometry(QtCore.QRect(100, 400, 51, 31))
  font = QtGui.QFont()
  font.setFamily("黑体")
  font.setPointSize(14)
  self.label_2.setFont(font)
  self.label_2.setObjectName("label_2")
  self.textBrowser = QtWidgets.QTextBrowser(self.centralWidget)
  self.textBrowser.setGeometry(QtCore.QRect(100, 110, 381, 271))
  self.textBrowser.setStyleSheet("background-color: rgb(242, 255, 233);")
  self.textBrowser.setObjectName("textBrowser")
  self.lineEdit = QtWidgets.QLineEdit(self.centralWidget)
  self.lineEdit.setGeometry(QtCore.QRect(160, 400, 211, 31))
  font = QtGui.QFont()
  font.setFamily("楷体")
  font.setPointSize(10)
  self.lineEdit.setFont(font)
  self.lineEdit.setText("")
  self.lineEdit.setObjectName("lineEdit")
  self.label = QtWidgets.QLabel(self.centralWidget)
  self.label.setGeometry(QtCore.QRect(240, 60, 151, 31))
  font = QtGui.QFont()
  font.setFamily("楷体")
  font.setPointSize(14)
  font.setBold(False)
  font.setWeight(50)
  self.label.setFont(font)
  self.label.setObjectName("label")
  self.pushButton_2 = QtWidgets.QPushButton(self.centralWidget)
  self.pushButton_2.setGeometry(QtCore.QRect(510, 140, 75, 23))
  self.pushButton_2.setObjectName("pushButton_2")
  self.pushButton_3 = QtWidgets.QPushButton(self.centralWidget)
  self.pushButton_3.setGeometry(QtCore.QRect(510, 220, 75, 23))
  self.pushButton_3.setObjectName("pushButton_3")
  self.pushButton_4 = QtWidgets.QPushButton(self.centralWidget)
  self.pushButton_4.setGeometry(QtCore.QRect(510, 310, 75, 23))
  self.pushButton_4.setObjectName("pushButton_4")
  self.graphicsView = QtWidgets.QGraphicsView(self.centralWidget)
  self.graphicsView.setGeometry(QtCore.QRect(0, 0, 661, 471))
  self.graphicsView.setStyleSheet("border-image: url(:/pic/Saved Pictures/f3cb924702022fc35eb6f865d67e23a6.jpg);")
  self.graphicsView.setObjectName("graphicsView")
  self.graphicsView.raise_()
  self.pushButton.raise_()
  self.label_2.raise_()
  self.textBrowser.raise_()
  self.lineEdit.raise_()
  self.label.raise_()
  self.pushButton_2.raise_()
  self.pushButton_3.raise_()
  self.pushButton_4.raise_()
  E_Dictory.setCentralWidget(self.centralWidget)
 
  self.retranslateUi(E_Dictory)
  QtCore.QMetaObject.connectSlotsByName(E_Dictory)
 
 def retranslateUi(self, E_Dictory):
  _translate = QtCore.QCoreApplication.translate
  E_Dictory.setWindowTitle(_translate("E_Dictory", "无道词典"))
  self.pushButton.setText(_translate("E_Dictory", "查找"))
  # 快捷键回车,可以使查找按钮发生效果
  self.pushButton.setShortcut(_translate("E_Dictory", "Return"))
  self.label_2.setText(_translate("E_Dictory", "单词:"))
  # setHtml样式表可以按照自己喜好修改
  self.textBrowser.setHtml(_translate("E_Dictory", "<!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:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
  self.label.setText(_translate("E_Dictory", "查询单词"))
  self.pushButton_2.setText(_translate("E_Dictory", "历史记录"))
  self.pushButton_3.setText(_translate("E_Dictory", "单词本"))
  self.pushButton_4.setText(_translate("E_Dictory", "添加单词"))
 
import dict_rc
 
if __name__ == "__main__":
 import sys
 app = QtWidgets.QApplication(sys.argv)
 E_Dictory = QtWidgets.QMainWindow()
 ui = Ui_E_Dictory()
 ui.setupUi(E_Dictory)
 E_Dictory.show()
 sys.exit(app.exec_())

textBrowser中的内容可以以html的格式显示出来,所有其中的文件的显示可以按照自己的喜好来设计。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
videocapture库制作python视频高速传输程序
Dec 23 Python
python数据结构之二叉树的遍历实例
Apr 29 Python
python实现下载指定网址所有图片的方法
Aug 08 Python
python批量制作雷达图的实现方法
Jul 26 Python
Python Django使用forms来实现评论功能
Aug 17 Python
Python进行数据提取的方法总结
Aug 22 Python
Python 操作MySQL详解及实例
Apr 30 Python
Python中执行存储过程及获取存储过程返回值的方法
Oct 07 Python
python2与python3中关于对NaN类型数据的判断和转换方法
Oct 30 Python
python 用下标截取字符串的实例
Dec 25 Python
django之自定义软删除Model的方法
Aug 14 Python
win10系统下python3安装及pip换源和使用教程
Jan 06 Python
python如何制作英文字典
Jun 25 #Python
Pandas DataFrame数据的更改、插入新增的列和行的方法
Jun 25 #Python
python模拟菜刀反弹shell绕过限制【推荐】
Jun 25 #Python
Pandas之DataFrame对象的列和索引之间的转化
Jun 25 #Python
Pandas之Fillna填充缺失数据的方法
Jun 25 #Python
PYQT5实现控制台显示功能的方法
Jun 25 #Python
Pandas之Dropna滤除缺失数据的实现方法
Jun 25 #Python
You might like
php下过滤html代码的函数 提高程序安全性
2010/03/02 PHP
PHP 图片上传代码
2011/09/13 PHP
php输出xml必须header的解决方法
2014/10/17 PHP
php+mysql删除指定编号员工信息的方法
2015/01/14 PHP
PHP单例模式详细介绍
2015/07/01 PHP
thinkPHP5框架闭包函数与子查询传参用法示例
2018/08/02 PHP
基于jquery的loading效果实现代码
2010/11/05 Javascript
深入理解JavaScript系列(4) 立即调用的函数表达式
2012/01/15 Javascript
利用json获取字符出现次数的代码
2012/03/22 Javascript
HTML5之lang属性与dir属性的详解
2013/06/19 Javascript
Javascript中克隆一个数组的实现代码
2013/12/06 Javascript
纯JavaScript代码实现文本比较工具
2016/02/17 Javascript
bootstrap布局中input输入框右侧图标点击功能
2016/05/16 Javascript
JS模拟的Map类实现方法
2016/06/17 Javascript
JS实现点击网页判断是否安装app并打开否则跳转app store
2016/11/18 Javascript
微信小程序 数组中的push与concat的区别
2017/01/05 Javascript
Bootstrap实现渐变顶部固定自适应导航栏
2020/08/27 Javascript
JavaScript之filter_动力节点Java学院整理
2017/06/28 Javascript
js断点调试经验分享
2017/12/08 Javascript
Angular2 自定义表单验证器的实现方法
2018/12/14 Javascript
weui中的picker使用js进行动态绑定数据问题
2019/11/06 Javascript
Python+Django在windows下的开发环境配置图解
2009/11/11 Python
Python修改MP3文件的方法
2015/06/15 Python
Python分治法定义与应用实例详解
2017/07/28 Python
Python魔法方法功能与用法简介
2019/04/04 Python
Python的matplotlib绘图如何修改背景颜色的实现
2019/07/16 Python
Python Django Cookie 简单用法解析
2019/08/13 Python
python爬虫利器之requests库的用法(超全面的爬取网页案例)
2020/12/17 Python
美国最受欢迎的度假租赁网站:VRBO
2016/08/02 全球购物
GLAMGLOW格莱魅美国官网:美国知名的面膜品牌
2016/12/31 全球购物
如何查找和删除数据库中的重复数据
2014/11/05 面试题
自强自立美德少年事迹材料
2014/08/16 职场文书
关于Vue Router的10条高级技巧总结
2021/05/06 Vue.js
Python实现打乒乓小游戏
2021/09/25 Python
如何解决php-fpm启动不了问题
2021/11/17 PHP
移除Selenium中window.navigator.webdriver值
2022/06/10 Python