|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
对面向对象编程不是太理解
下面的例子怎样改成面向对象编程,求思路
import tkinter as tk
import requests
import json
root = tk.Tk()
def operation():
content = e1.get()
if content == '':
pass
else:
e2.delete(0, tk.END)
v2.set(translation(content))
def translation(content):
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
data = {}
data['i'] = content
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '1542686477843'
data['sign'] = 'ff690fdd9bcfe53ac87a76bd7690cea3'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_REALTIME'
data['typoResult'] = 'false'
response = requests.post(url,data=data)
html = json.loads(response.text)
return html['translateResult'][0][0]['tgt']
frame = tk.LabelFrame(root, text='这是一个小小的翻译程序')
frame.pack(padx=5, pady=5, expand=1)
label1 = tk.Label(frame, text='输入要翻译的内容:')
label2 = tk.Label(frame, text='结果:')
label1.grid(row=0, column=0)
label2.grid(row=1, column=0)
v1 = tk.StringVar()
v2 = tk.StringVar()
e1 = tk.Entry(frame, textvariable=v1, width=30)
e2 = tk.Entry(frame, textvariable=v2, width=30, state='readonly')
e1.grid(row=0, column=1, padx=10, pady=5)
e2.grid(row=1, column=1, padx=10, pady=5)
button1 = tk.Button(frame, text='翻译', command=operation, width=10)
button2 = tk.Button(frame, text='退出', command=root.quit, width=10)
button1.grid(sticky=tk.W, row=2, column=0, padx=10, pady=5)
button2.grid(sticky=tk.E, row=2, column=1, padx=10, pady=5)
root.mainloop() |
|