import tkinter as tk
import requests
import urllib.request
import urllib.parse
import re
import json
def __init__(self, master=none):
tk.frame.__init__(self, master)
self.grid()
self.createwidgets()
def createwidgets(self):
#1self.lbltitle = tk.label(self, text="請輸入你想要翻譯文字:")
self.lbltitle.grid(row=0, column=0 )
self.edittext = tk.text(self, width=40, height=12)
self.edittext.grid(row=1, column=0)
#按鈕self.btnok = tk.button(self, text='翻譯',command=self.youdaotranslte)
self.btnok.grid(row=0, column=0, columnspan=8)
# root.bind('', self.youdaotranslte) #失敗 ovo, 增加快捷鍵失敗
#2還可以新增對已存在視窗的翻譯結果刪除,再新增新的翻譯結果的功能
self.lblresul = tk.label(self, text="翻譯的結果:")
self.lblresul.grid(row=0, column=1)
self.resulttext = tk.text(self, width=40, height=12)
self.resulttext.grid(row=1, column=1)
#root.bind('', self.youdaotranslte)
def youdaotranslte(self, event=none):
request_url = ''
form_data = {}
form_data['doctype'] = 'json' #can't delete,return type
form_data['i'] = self.edittext.get(0.0, tk.end) #print(form_data['i'])
data = urllib.parse.urlencode(form_data).encode('utf-8')
response = urllib.request.urlopen(request_url,data)
html = response.read().decode('utf-8')
translate_results = json.loads(html)
translate_results = translate_results['translateresult'][0][0]['tgt']
#print("the translate result is:%s" % translate_results)
self.resulttext.insert(tk.end, translate_results)
root = tk.tk()
root.title('簡易翻譯器')
root.geometry("580x200+350+140")
執行結果顯示:
Python 基於有道翻譯的簡單文字翻譯器
基於有道翻譯的簡單文字翻譯器import requests from bs4 import beautifulsoup import tkinter as tk from tkinter import ttk import re defgetresult sentin,form headers po...
使用Urllib2製作有道翻譯器
1.分析post請求的url 2.檢視form data 其為post請求的資料,將其粘帖到sublime編輯器下,使用正則替換,將其轉化為字典的形式 i python from auto to auto smartresult dict client fanyideskweb salt 15297...
學習爬蟲有道翻譯中遇到的
import urllib.request import urllib.parse import json from tkinter import root tk root.wm attributes topmost 1 root.geometry 1000 40 root.minsize 300,...