tkinter的功能是如此强大,竟然还能做翻译软件。当然是在线的,我发现有一个quicktranslate模块,可以提供在线翻译功能,相当于提供了一个翻译的接口,利用它就可以制作在线翻译软件了。下面是代码,分享给大家。
注意要首先 pip install quicktranslate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
#-*- coding:utf-8 -*- import tkinter as tk #使用tkinter前需要先导入 from tkinter import messagebox,ttk import datetime,os,time,re,requests from quicktranslate import get_translate_baidu,get_translate_youdao class my_gui(tk.tk): #初始化 def __init__( self ): super ().__init__() self .set_window() self .set_frame() self .set_body() #设置窗口 def set_window( self ): self .title( "文本翻译工具v1.0" ) #窗口名 w_width = 800 #工具宽度 w_height = 600 #工具高度 scn_width = self .maxsize()[ 0 ] #屏幕宽度 x_point = (scn_width - w_width) / / 2 #取点让工具居中 self .geometry( '%dx%d+%d+%d' % (w_width,w_height,x_point, 100 )) #设置框架 def set_frame( self ): self .frame1 = tk.frame( self ,pady = 15 ,padx = 15 ) self .frame1.grid(row = 0 ,column = 0 ,sticky = 'w' ) self .frame2 = tk.frame( self ,pady = 15 ,padx = 15 ) self .frame2.grid(row = 1 ,column = 0 ,sticky = 'w' ) self .frame3 = tk.frame( self ,pady = 15 ,padx = 15 ) self .frame3.grid(row = 2 ,column = 0 ,sticky = 'w' ) #设置组件 def set_body( self ): ttk.label( self .frame1,text = '输入要翻译的内容:' ).grid(row = 0 ,column = 0 ,sticky = 'w' ) ybar = ttk.scrollbar( self .frame1,orient = 'vertical' ) textarea = tk.text( self .frame1,width = 100 ,height = 12 ,yscrollcommand = ybar. set ) ybar[ 'command' ] = textarea.yview textarea.grid(row = 1 ,column = 0 ,columnspan = 2 ) ybar.grid(row = 1 ,column = 2 ,sticky = 'ns' ) textarea.bind( "<double-button-1>" , lambda event: self .getword(event,textarea)) #双击触发单词翻译 ttk.button( self .frame1,text = '点击翻译' ,command = lambda : self .translator(textarea.get( 1.0 , 'end' ))).grid(row = 2 ,column = 0 ,sticky = 'w' ) ttk.button( self .frame1,text = '清空' ,command = lambda : self .clear(textarea)).grid(row = 2 ,column = 1 ,sticky = 'w' ) #功能函数 def clear( self ,textarea): #清空文本框 textarea.delete( 1.0 , 'end' ) def is_cn( self ,uchar): """判断一个unicode是否是汉字""" if uchar > = u '\u4e00' and uchar< = u '\u9fa5' : return true else : return false def filterchar( self ,char): #过滤字母 word = filter ( str .isalpha, char) word = ''.join( list (word)) return word def getword( self ,event,textarea): #单词翻译 place = textarea.index( 'current' ) row = int (place.split( '.' )[ 0 ]) #第几行 col = int (place.split( '.' )[ 1 ]) #第几列 col_start = col - 15 col_end = col + 15 part1 = textarea.get( str (row) + '.' + str (col_start), str (row) + '.' + str (col)).split()[ - 1 ] part2 = textarea.get( str (row) + '.' + str (col), str (row) + '.' + str (col_end)).split()[ 0 ] word_f = self .filterchar(part1 + part2) word_t = get_translate_baidu(word_f) #百度翻译 if (word_t = = 'wrong!' ): word_t = get_translate_youdao(word_f) #百度不行就用有道翻译 textarea = tk.text( self .frame3,width = 100 ,height = 8 ) #第三个框架添加文本框 textarea.grid(row = 0 ,column = 0 ) textarea.delete( 1.0 , 'end' ) textarea.insert( 1.0 ,word_f + ':\n' + word_t) #文本框填入翻译结果 #生词写入生词本 if (word_t! = 'wrong!' ): with open ( 'word.txt' , 'a' ) as f: f.write(word_f + ' : ' + word_t + "\n" ) def translator( self ,content): #段落翻译 test_url = 'http://youdao.com' try : requests.get(test_url,timeout = 2 ) except : messagebox.showerror( '警告' , '没有联网' ) if ( self .is_cn(content)): #如果输入的是中文,那就是中译英,要用英文句号 sep = "." else : sep = "。" contents = content.split( '\n' ) #段落分割 strs = "" for paragraph in contents: if (paragraph): sentences = paragraph.split( '.' ) #句子 for sentence in sentences: if (sentence): res = get_translate_youdao(sentence) #有道翻译 if (res = = 'wrong!' ): res = get_translate_google(sentence) #有道不行就用谷歌翻译 strs + = res + sep strs + = "\n" ybar = ttk.scrollbar( self .frame2,orient = 'vertical' ) textarea = tk.text( self .frame2,width = 100 ,height = 12 ,yscrollcommand = ybar. set ) #第二个框架添加文本框 textarea.bind( "<double-button-1>" , lambda event: self .getword(event,textarea)) #双击触发单词翻译 如果是中译英 ybar[ 'command' ] = textarea.yview textarea.grid(row = 0 ,column = 0 ) textarea.delete( 1.0 , 'end' ) textarea.insert( 1.0 ,strs) #文本框填入翻译结果 ybar.grid(row = 0 ,column = 1 ,sticky = 'ns' ) app = my_gui() app.mainloop() |
这是效果,双击单词可自动查词,段落翻译默认用有道,不行用谷歌;单词翻译默认百度,不行用有道。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/chaodaibing/article/details/113925474