python利用mysql数据库实现一个中英文翻译程序兼单词试卷生成并改阅功能,并可以爬取有道官网进行在线翻译。

您所在的位置:网站首页 下载一个中英翻译 python利用mysql数据库实现一个中英文翻译程序兼单词试卷生成并改阅功能,并可以爬取有道官网进行在线翻译。

python利用mysql数据库实现一个中英文翻译程序兼单词试卷生成并改阅功能,并可以爬取有道官网进行在线翻译。

2024-07-18 06:31:52| 来源: 网络整理| 查看: 265

直接上运行截图(代码在后面)

开始界面 在这里插入图片描述

使用navicat查看mysql数据库中的数据

在这里插入图片描述

单词查询功能测试

本地翻译一个本地数据库中有的单词

在这里插入图片描述 本地翻译一个本地数据库中没有的单词 在这里插入图片描述 爬取有道官网进行在线翻译 在这里插入图片描述

单词录入功能测试

我是录入了很多单词,这里只放一张图片 在这里插入图片描述 然后查看数据库中的数据 在这里插入图片描述

单词测试功能测试

点击单词测试结果如下 在这里插入图片描述 难度选择中等后点击生成结果如下 在这里插入图片描述 在下面的输入框中输入你的答案然后点击提交,程序将会把你的试卷改出来 在这里插入图片描述

注意事项

这个程序是我好久以前用当时的电脑做的,现在电脑中本程序所需要的的数据库表并不全,大家如果需要使用的话,可以根据我的代码自己创建数据库,程序中的所有功能都是可以正常使用的。

源代码(如转发请注明出处,谢谢) # -*- coding: utf-8 -*- """ Created on Tue Dec 24 13:06:14 2019 @author: 一叶落而知天下秋 """ import random import wx import pymysql from urllib import request, parse import json import pymysql class priframe(wx.Frame): def __init__(self,sup): wx.Frame.__init__(self,parent=sup,title='多多瑜教育管理中心',pos=(650,300),size=(900,600)) panel=wx.Panel(self) image_file = '背景2.png' to_bmp_image = wx.Image(image_file, wx.BITMAP_TYPE_ANY).ConvertToBitmap() panel.bitmap = wx.StaticBitmap(panel,-1, to_bmp_image, (0, 0)) #set_title = '%s %d x %d' % (image_file,200,400) #self.SetTitle(set_title) '''panel.Bind(wx.EVT_ERASE_BACKGROUND,self.OnEraseBack) def OnEraseBack(self,event): dc = event.GetDC() if not dc: dc = wx.ClientDC(self) rect = self.GetUpdateRegion().GetBox() dc.SetClippingRect(rect) dc.Clear() bmp = wx.Bitmap("1.jpg") dc.DrawBitmap(bmp, 0, 0)''' #wx.StaticText(parent=panel,label='欢迎来到多多瑜教育管理中心',pos=(20,20)) bmp=wx.Image('背景21.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap() #self.button = wx.BitmapButton(panel, -1, bmp, pos=(10, 20)) self.buttondancichaxun=wx.Button(panel.bitmap,label='单词查询',pos=(500,250),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttondancichaxun,self.buttondancichaxun) self.buttondanciluru=wx.Button(parent=panel.bitmap,label='单词录入',pos=(580,250),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttondanciluru,self.buttondanciluru) self.buttondancixiugai=wx.Button(parent=panel.bitmap,label='单词修改',pos=(660,250),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttondancixiugai,self.buttondancixiugai) self.buttondancishanchu=wx.Button(parent=panel.bitmap,label='单词删除',pos=(740,250),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttondancishanchu,self.buttondancishanchu) self.buttonjiansuojilu=wx.Button(parent=panel.bitmap,label='检索记录',pos=(420,370),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttonjiansuojilu,self.buttonjiansuojilu) self.buttondanciceshi=wx.Button(parent=panel.bitmap,label='单词测试',pos=(500,370),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttondanciceshi,self.buttondanciceshi) self.buttonweizhijiansuo=wx.Button(parent=panel.bitmap,label='位置检索',pos=(420,310),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttonweizhijiansuo,self.buttonweizhijiansuo) self.buttonlijuchaxun=wx.Button(parent=panel.bitmap,label='例句查询',pos=(500,310),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttonlijuchaxun,self.buttonlijuchaxun) self.buttonlijuluru=wx.Button(parent=panel.bitmap,label='例句录入',pos=(580,310),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttonlijuluru,self.buttonlijuluru) self.buttonlijuxiugai=wx.Button(parent=panel.bitmap,label='例句修改',pos=(660,310),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttonlijuxiugai,self.buttonlijuxiugai) self.buttonlijushanchu=wx.Button(parent=panel.bitmap,label='例句删除',pos=(740,310),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttonlijushanchu,self.buttonlijushanchu) self.buttonduanyuchaxun=wx.Button(parent=panel.bitmap,label='短语查询',pos=(580,370),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttonduanyuchaxun,self.buttonduanyuchaxun) self.buttonbeifen=wx.Button(parent=panel.bitmap,label='数据备份',pos=(660,370),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttonbeifen,self.buttonbeifen) self.buttonhuifu=wx.Button(parent=panel.bitmap,label='数据恢复',pos=(740,370),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttonhuifu,self.buttonhuifu) self.buttonwenjianchuangjian=wx.Button(panel.bitmap,label='创建文件',pos=(420,250),size=(70,40)) self.Bind(wx.EVT_BUTTON,self.onbuttonwenjianchuangjian,self.buttonwenjianchuangjian) def onbuttonbeifen(self,event): conn=pymysql.connect(host='localhost',user='root',passwd='123456',db='sys') conn2=pymysql.connect(host='localhost',user='root',passwd='123456',db='sys') cur=conn.cursor() cur2=conn2.cursor() cur2.execute('delete from Wordbeifen') print('123') #row=cur.fetchone() #shuliang=row[0] cur.execute('select * from Word') row22=cur.fetchone() print(row22) while row22: cur2.execute('insert into Wordbeifen(danci,fanyi,difficulty) values(%s,%s,%d)',(row22[0],row22[1],row22[2])) row22=cur.fetchone() print(row22) conn.commit() conn2.commit() cur.close() cur2.close() conn.close() conn2.close() beifenframe(None).Show() def onbuttonhuifu(self,event): conn=pymysql.connect(host='localhost',user='root',passwd='123456',db='sys') conn2=pymysql.connect(host='localhost',user='root',passwd='123456',db='sys') cur=conn.cursor() cur2=conn2.cursor() cur2.execute('delete from Word') print('123') #row=cur.fetchone() #shuliang=row[0] cur.execute('select * from Wordbeifen') row22=cur.fetchone() print(row22) while row22: cur2.execute('insert into Word(danci,fanyi,difficulty) values(%s,%s,%d)',(row22[0],row22[1],row22[2])) row22=cur.fetchone() print(row22) conn.commit() conn2.commit() cur.close() cur2.close() conn.close() conn2.close() huifuframe(None).Show() def onbuttondancichaxun(self,event): dancichaxunframe(None).Show() def onbuttondanciluru(self,event): danciluruframe(None).Show() def onbuttondancixiugai(self,event): dancixiugaiframe(None).Show() def onbuttondancishanchu(self,event): dancishanchuframe(None).Show() def onbuttondanciceshi(self,event): danciceshiframe(None).Show() def onbuttonlijuchaxun(self,event): lijuchaxunframe(None).Show() def onbuttonlijuluru(self,event): lijuluruframe(None).Show() def onbuttonlijuxiugai(self,event): lijuxiugaiframe(None).Show() def onbuttonlijushanchu(self,event): lijushanchuframe(None).Show() def onbuttonduanyuchaxun(self,event): duanyuchaxunframe(None).Show() def onbuttonwenjianchuangjian(self,event): wenjianchuangjianframe(None).Show() def onbuttonweizhijiansuo(self,event): weizhijiansuoframe(None).Show() def onbuttonjiansuojilu(self,event): jiansuojiluframe(None).Show() class beifenframe(wx.Frame): def __init__(self,sup): wx.Frame.__init__(self,parent=sup,title='数据备份',size=(200,150),pos=(650,100)) panel=wx.Panel(self) self.result=wx.StaticText(parent=panel,label='数据备份成功',pos=(20,20),size=(150,20)) class huifuframe(wx.Frame): def __init__(self,sup): wx.Frame.__init__(self,parent=sup,title='数据恢复',size=(200,150),pos=(650,100)) panel=wx.Panel(self) self.result=wx.StaticText(parent=panel,label='数据恢复成功',pos=(20,20),size=(150,20)) class dancichaxunframe(wx.Frame): def __init__(self,sup): wx.Frame.__init__(self,parent=sup,title='单词查询',size=(400,300),pos=(650,100)) panel=wx.Panel(self) image_file = '通用背景1.jpg' to_bmp_image = wx.Image(image_file, wx.BITMAP_TYPE_ANY).ConvertToBitmap() #panel.bitmap = wx.StaticBitmap(panel,-1, to_bmp_image, (0, 0)) self.text=wx.StaticText(parent=panel,label='请输入你想要查询的单词:',pos=(20,20),size=(200,20)) #font = wx.Font(12, wx.DECORATIVE, wx.ITALIC, wx.NORMAL) #self.text.SetFont(font) #self.text.SetForegroundColour('blue') self.inputdanci=wx.TextCtrl(parent=panel,pos=(250,20),size=(100,20)) self.result=wx.StaticText(parent=panel,label='',pos=(20,80),size=(200,20)) self.result.SetBackgroundColour('lucency') self.buttonchaxun=wx.Button(parent=panel,label='英译汉',pos=(20,200)) self.Bind(wx.EVT_BUTTON,self.onbuttonchaxun,self.buttonchaxun) self.buttonchaxun2=wx.Button(parent=panel,label='汉译英',pos=(120,200)) self.Bind(wx.EVT_BUTTON,self.onbuttonchaxun2,self.buttonchaxun2) self.buttonzaixianfanyi=wx.Button(parent=panel,label='在线翻译',pos=(220,200)) self.Bind(wx.EVT_BUTTON,self.onbuttonzaixianfanyi,self.buttonzaixianfanyi) def onbuttonchaxun(self,event): self.result.SetLabel('') string=str(self.inputdanci.GetValue()) if string=='': self.result.SetLabel("输入不能为空,请重新输入") else: conn=pymysql.connect(host='localhost',port=3306,user='root',passwd='123456',db='sys') cur=conn.cursor() cur.execute('SELECT * FROM Word where word=%s',string) row = cur.fetchone() if row: string=row[1] self.result.SetLabel('本地翻译的中文为:'+string) else: self.result.SetLabel("本地词库中不存在该单词,请重新输入") cur.close() conn.close() def onbuttonchaxun2(self,event): self.result.SetLabel('') string=str(self.inputdanci.GetValue()) if string=='': self.result.SetLabel("输入不能为空,请重新输入") else: conn=pymysql.connect(host='localhost',port=3306,user='root',passwd='123456',db='sys') cur=conn.cursor() cur.execute('SELECT * FROM Word where translate=%s',string) row = cur.fetchone() if row: string=row[0] self.result.SetLabel('本地词库的英文翻译为:'+string) else: self.result.SetLabel("该中文不存在,请重新输入") cur.close() conn.close() def onbuttonzaixianfanyi(self,event): if str(self.inputdanci.GetValue())=='': self.result.SetLabel('输入不能为空,请重新输入') else: req_url = 'http://fanyi.youdao.com/translate' # 创建连接接口 # 创建要提交的数据 Form_Date = {} Form_Date['i'] =str(self.inputdanci.GetValue()) # 要翻译的内容可以更改 Form_Date['doctype'] = 'json' data = parse.urlencode(Form_Date).encode('utf-8') #数据转换 response = request.urlopen(req_url, data) #提交数据并解析 html = response.read().decode('utf-8') #服务器返回结果读取 #print(html) # 可以看出html是一个json格式 translate_results = json.loads(html) #以json格式载入 translate_results = translate_results['translateResult'][0][0]['tgt'] # json格式调取 self.result.SetLabel('有道在线翻译为:'+translate_results) #输出结果 class lijuchaxunframe(wx.Frame): def __init__(self,sup): wx.Frame.__init__(self,parent=sup,title='例句查询',size=(400,300),pos=(650,100)) panel=wx.Panel(self) wx.StaticText(parent=panel,label='请输入你想要查询例句的单词:',pos=(20,20),size=(220,20)) self.inputdanci=wx.TextCtrl(parent=panel,pos=(20,60)) self.result=wx.StaticText(parent=panel,label='',pos=(20,100),size=(300,20)) self.buttonchaxun=wx.Button(parent=panel,label='查询',pos=(20,200)) self.Bind(wx.EVT_BUTTON,self.onbuttonchaxun,self.buttonchaxun) #self.buttonzaixianfanyi=wx.Button(parent=panel,label='在线翻译',pos=(120,200)) #self.Bind(wx.EVT_BUTTON,self.onbuttonzaixianfanyi,self.buttonzaixianfanyi) def onbuttonchaxun(self,event): self.result.SetLabel('') string=str(self.inputdanci.GetValue()) if str(self.inputdanci.GetValue())=='': self.result.SetLabel("输入不能为空,请重新输入") else: conn=pymysql.connect(host='localhost',port=3306,user='root',passwd='123456',db='sys') cur=conn.cursor() cur.execute('SELECT liju FROM lijubiao where danci=%s',string) row = cur.fetchone() if row: string=row[0] if string!='': self.result.SetLabel('该单词的例句:'+string) else: self.result.SetLabel('本地数据库没有该单词的例句') else: self.result.SetLabel("该单词不存在,请重新输入") cur.close() conn.close() class duanyuchaxunframe(wx.Frame): def __init__(self,sup): wx.Frame.__init__(self,parent=sup,title='短语查询',size=(400,300),pos=(650,100)) panel=wx.Panel(self) wx.StaticText(parent=panel,label='请输入你想要查询的短语:',pos=(20,20),size=(300,15)) self.inputdanci=wx.TextCtrl(parent=panel,pos=(20,60),size=(300,20)) self.result=wx.StaticText(parent=panel,label='',pos=(20,100),size=(300,20)) self.buttonchaxun=wx.Button(parent=panel,label='本地查询',pos=(20,200)) self.Bind(wx.EVT_BUTTON,self.onbuttonchaxun,self.buttonchaxun) self.buttonzaixianfanyi=wx.Button(parent=panel,label='在线翻译',pos=(120,200)) self.Bind(wx.EVT_BUTTON,self.onbuttonzaixianfanyi,self.buttonzaixianfanyi) def onbuttonchaxun(self,event): self.result.SetLabel('') string=str(self.inputdanci.GetValue()) if str(self.inputdanci.GetValue())=='': self.result.SetLabel("输入不能为空,请重新输入") else: conn=pymysql.connect(host='localhost',port=3306,user='root',passwd='123456',db='sys') cur=conn.cursor() cur.execute('SELECT fanyi FROM duanyubiao where duanyu=%s',string) row = cur.fetchone() if row: string=row[0] self.result.SetLabel('本地翻译为:'+string) else: self.result.SetLabel("该单词不存在,请重新输入") cur.close() conn.close() def onbuttonzaixianfanyi(self,event): if str(self.inputdanci.GetValue())=='': self.result.SetLabel("输入不能为空,请重新输入") else: req_url = 'http://fanyi.youdao.com/translate' # 创建连接接口 # 创建要提交的数据 Form_Date = {} Form_Date['i'] =str(self.inputdanci.GetValue()) # 要翻译的内容可以更改 Form_Date['doctype'] = 'json' data = parse.urlencode(Form_Date).encode('utf-8') #数据转换 response = request.urlopen(req_url, data) #提交数据并解析 html = response.read().decode('utf-8') #服务器返回结果读取 #print(html) # 可以看出html是一个json格式 translate_results = json.loads(html) #以json格式载入 translate_results = translate_results['translateResult'][0][0]['tgt'] # json格式调取 self.result.SetLabel('有道在线翻译为:'+translate_results) #输出结果 class danciluruframe(wx.Frame): def __init__(self,sup): wx.Frame.__init__(self,parent=sup,title='单词录入',pos=(650,300),size=(400,300)) panel=wx.Panel(self) wx.StaticText(parent=panel,label='请分别输入所要录入的单词以及翻译:',pos=(20,20),size=(300,15)) wx.StaticText(parent=panel,label='单词:',pos=(20,50),size=(50,20)) wx.StaticText(parent=panel,label='翻译:',pos=(20,80),size=(50,20)) self.inputdanci=wx.TextCtrl(parent=panel,pos=(80,50),size=(200,20)) self.inputfanyi=wx.TextCtrl(parent=panel,pos=(80,80),size=(200,20)) self.buttonluru=wx.Button(parent=panel,label='录入',pos=(20,190),size=(100,30)) self.Bind(wx.EVT_BUTTON,self.onbuttonluru,self.buttonluru) self.result=wx.StaticText(parent=panel,label='',pos=(20,150),size=(300,20)) def onbuttonluru(self,event): string1=str(self.inputdanci.GetValue()) string2=str(self.inputfanyi.GetValue()) if string1=='' or string2=='': self.result.SetLabel('录入失败,请输入正确格式英文单词以及翻译') else: conn=pymysql.connect(host='localhost',port=3306,user='root',passwd='123456',db='sys') cur=conn.cursor() #cur2=conn.cursor() if len(string1)


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


    图片新闻

    实验室药品柜的特性有哪些
    实验室药品柜是实验室家具的重要组成部分之一,主要
    小学科学实验中有哪些教学
    计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
    实验室各种仪器原理动图讲
    1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
    高中化学常见仪器及实验装
    1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
    微生物操作主要设备和器具
    今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
    浅谈通风柜使用基本常识
     众所周知,通风柜功能中最主要的就是排气功能。在

    专题文章

      CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭