首页 技术 正文
技术 2022年11月19日
0 收藏 855 点赞 3,533 浏览 1305 个字

1.图片展示:

python-利用Python窗口可视化抽象开发山寨版翻译软件

python-利用Python窗口可视化抽象开发山寨版翻译软件

2.写出上面图式的小脚本需要利用python两个方面的知识:

(1)可视化库 (需用库:tkinter)

(2)简单爬虫知识 (需用库:requests)

注意:爬虫在获取翻译信息时,会因为一些反爬虫的机制导致失败,所以从翻译网站获取翻译信息时,要选择稳定性强的翻译网站,并做好针对网站的反爬机制做好措施,防止出错。该脚本用的翻译网站是随意找的,因而容易出错,建议用百度翻译、有道翻译等一些好的翻译网站。

3.代码如下:

from tkinter import *
import requests# 在线翻译网站 post:f t w
start_url = "http://fy.iciba.com/ajax.php?a=fy"
# 创建窗口
root = Tk()
# 窗口标题
root.title("中英互译")
# 窗口大小
root.geometry("370x100+500+300")
# 标签控件
label1 = Label(root, text="输入要翻译的文字:")
label1.grid(row=0, column=0)
label2 = Label(root, text="翻译之后的结果:")
label2.grid(row=1, column=0)
# 输入控件
entry1 = Entry(root, font=("微软雅黑", 15))
entry1.grid(row=0, column=1)
entry2 = Entry(root, font=("微软雅黑", 15))
entry2.grid(row=1, column=1)# 按钮
def translate():
entry2.delete(0, 'end')
input_data = entry1.get()
if not input_data.isalpha():
input_data = input_data.lower()
data = {
"f": "auto",
"t": "auto",
"w": input_data
}
response = requests.post(start_url, data=data)
result = response.json()
output = result['content']['out']
else:
data = {
"f": "auto",
"t": "auto",
"w": input_data
}
response = requests.post(start_url, data=data)
result = response.json()
output = result['content']['word_mean'][0]
entry2.insert("insert", output)button1 = Button(root, text="翻译", width=10, command=translate)
# sticky(对齐方式):N S W E
button1.grid(row=2, column=0, sticky=W)
button1 = Button(root, text="退出", width=10, command=root.quit)
button1.grid(row=2, column=1, sticky=E)
# 显示窗口 消息循环
root.mainloop()
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898