小白学tkinter(第一个程序 有道翻译)

效果图

小白学tkinter(第一个程序 有道翻译)_第1张图片

布局

小白学tkinter(第一个程序 有道翻译)_第2张图片
悲催啊,做了好久才发现,Text()组件没有textvariable属性。。。。。换了种方式。。。。。

from tkinter import *
import urllib.request  
import urllib.parse  
import json


root = Tk()
root.title('有道翻译1.0')
root.resizable(0,0)
#调用翻译函数
def translate(content):  
     url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=dict2.index'  

     head={}  
     head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883'  
     data={}  
     data['type']='AUTO'  
     data['i']= content  
     data['doctype']= 'json'  
     data['xmlVersion']= '1.8'  
     data['keyfrom']= 'fanyi.web'  
     data['ue']= 'UTF-8'  
     data['action']= 'FY_BY_CLICKBUTTON'  
     data['typoResult']='true'  
     data=urllib.parse.urlencode(data).encode('utf-8')  
     req=urllib.request.Request(url,data,head)  
     response=urllib.request.urlopen(req)  
     html=response.read().decode('utf-8')  
     target=json.loads(html)  
     result = target['translateResult'][0][0]['tgt']  
     return result

#调用清除函数
def clear():
    return text_1.delete(1.0,END)
#调用翻译函数
def trans():
    text_2.insert(INSERT,translate(text_1.get(1.0,END)))

#调用右键text_1相关函数(这个地方标记一下)
def selectAll():
     text_1.tag_add('sel',1.0,END)
def copy():
     text_1.event_generate('<>')
def paste():
     text_1.event_generate('<>')
def cut():
     text_1.event_generate('<>')
#调用右键text_2
def selectAll_1():
     text_2.tag_add('sel',1.0,END)
def copy_1():
     text_2.event_generate('<>')
def paste_1():
     text_2.event_generate('<>')
def cut_1():
     text_2.event_generate('<>')

#设置Frame
frame_1 = Frame(root,width = 20,height = 5)
frame_2 = Frame(root,width = 20,height = 20)
frame_3 = Frame(root,width = 20,height = 5)



#组件Scrollbar
scr_1 = Scrollbar(frame_1)
scr_2 = Scrollbar(frame_3)


#组件Label
label_1 = Label(frame_1,text = '原文')
label_2 = Label(frame_3,text = '译文')



#组件Text
text_1 = Text(frame_1,height = 15,yscrollcommand = scr_1.set,font = ('宋体',12))
scr_1.config(command = text_1.yview)
text_2 = Text(frame_3,height = 15,yscrollcommand = scr_2.set,fg = 'blue',font = ('宋体',12))
scr_2.config(command = text_2.yview)

#组件Button
button_1 = Button(frame_2,text = '翻译',command = trans)
button_2 = Button(frame_2,text = '清除',command = clear)


#组件text_1绑定Menu右键菜单
menu = Menu(text_1,tearoff = False)
menu.add_command(label = '全选',command = selectAll)
menu.add_command(label = '复制',command = copy)
menu.add_command(label = '剪切',command = cut)
menu.add_command(label = '粘贴',command = paste)
def popup(event):
     menu.post(event.x_root,event.y_root)
text_1.bind('',popup)


#组件text_2绑定Menu右键菜单
menu_1 = Menu(text_2,tearoff = False)
menu_1.add_command(label = '全选',command = selectAll_1)
menu_1.add_command(label = '复制',command = copy_1)
menu_1.add_command(label = '剪切',command = cut_1)
menu_1.add_command(label = '粘贴',command = paste_1)
def popup(event):
     menu_1.post(event.x_root,event.y_root)
text_2.bind('',popup)

#布局frame
frame_1.grid(row = 0,column = 0)
frame_2.grid(row = 1,column = 0,padx = 10)
frame_3.grid(row = 2,column = 0,pady = 10)

#布局组件
label_1.grid(row = 0,column = 0,padx = 15,sticky=W)
label_2.grid(row = 3,column = 0,padx = 15,sticky=W)


#布局Button
button_1.grid(row = 3,column = 1,padx = 10,pady = 10,sticky = E)
button_2.grid(row = 3,column = 2,padx = 10,pady = 10,sticky = E)
#布局文本框
text_1.grid(row = 1,column = 0,padx = 10)
text_2.grid(row = 5,column = 0,padx = 10)
#布局Scrollbar
scr_1.grid(row = 1,column =1,sticky=N+S+E)
scr_2.grid(row = 5,column =1,sticky=N+S+E)

root.mainloop()

就是有时候翻译的内容不全,不知道哪的问题!~

你可能感兴趣的:(Tkinter学习)