鱼C论坛

 找回密码
 立即注册
查看: 647|回复: 1

请教面向对象的编程思路

[复制链接]
发表于 2018-11-29 13:29:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-11-29 14:06:54 | 显示全部楼层
面向对象无非是定义类,函数什么的,你是想把哪个通用化
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-19 03:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表