赞
踩
#http://www.cnblogs.com/kaituorensheng/p/3287652.html(可视化 tkinter模块)、
#在线翻译:
import urllib.request
import urllib.parse
import json
from tkinter import *
from tkinter import ttk
class Translations(object):
def __init__(self):
self.root = Tk()
self.root.title('Python翻译')
self.root.resizable(width = False, height = False)
self.s1 = ttk.Scrollbar(self.root)
self.s2 = ttk.Scrollbar(self.root)
self.text_1=Text(self.root,width=35,height=8,padx=10,pady=10,font=("微软雅黑", 13), relief = FLAT, yscrollcommand = self.s1.set)
self.text_2=Text(self.root,width=35,height=8,padx=10,pady=10,font=("微软雅黑", 13), relief = FLAT, yscrollcommand = self.s2.set)
self.right_click_menu_t1()
self.right_click_menu_t2()
self.quick_key()
self.text_1.grid( row = 1, column =0, columnspan = 2)
self.text_2.grid( row = 4, column = 0, columnspan = 2)
self.s1.grid(row = 1, column = 2, sticky = N + S)
self.s2.grid(row = 4, column = 2, sticky = N + S)
self.s1.config(command = self.text_1.yview)
self.s2.config(command = self.text_2.yview)
Label(self.root, text = '原文', fg = 'gray').grid(row = 0, sticky = W)
Label(self.root, text = '译文', fg = 'gray').grid(row = 2, sticky = W)
button_1 = Button(self.root, text = '翻译', font = ('微软雅黑', 8), command = self.translate, width = 10, relief = GROOVE)
button_2 = Button(self.root, text = '清除', font = ('微软雅黑', 8), command = self.textclear, width = 10, relief = GROOVE)
button_1.grid(padx = 2, pady = 2, row = 2, column = 0, sticky = E)
button_2.grid(padx = 2, pady = 2, row = 2, column = 1, sticky = W)
self.root.mainloop()
def copytext_1(self):
def select():
try:
return self.text_1.get(SEL_FIRST, SEL_LAST)
except Exception:
return ''
aString = select()
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_TEXT, aString.encode('gbk'))
w.CloseClipboard()
def getText_1(self):
w.OpenClipboard()
d = w.GetClipboardData(win32con.CF_TEXT)
w.CloseClipboard()
self.text_1.insert(INSERT, d.decode('gbk'))
def copytext_2(self):
def select():
try:
return self.text_2.get(SEL_FIRST, SEL_LAST)
except Exception:
return ''
aString = select()
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_TEXT, aString.encode('gbk'))
w.CloseClipboard()
def getText_2(self):
w.OpenClipboard()
d = w.GetClipboardData(win32con.CF_TEXT)
w.CloseClipboard()
self.text_2.insert(INSERT, d.decode('gbk'))
def right_click_menu_t1(self):
def right_menu(event):
rightmenu.post(event.x_root, event.y_root)
rightmenu = Menu(self.root, tearoff = False)
rightmenu.add_command(label = '复制', command = self.copytext_1)
rightmenu.add_command(label = '粘贴', command = self.getText_1)
rightmenu.add_command(label = '清除', command = self.textclear)
self.text_1.bind('<Button-3>', right_menu)
def right_click_menu_t2(self):
def right_menu(event):
rightmenu.post(event.x_root, event.y_root)
rightmenu = Menu(self.root, tearoff = False)
rightmenu.add_command(label = '复制', command = self.copytext_2)
rightmenu.add_command(label = '粘贴', command = self.getText_2)
rightmenu.add_command(label = '清除', command = self.textclear)
self.text_2.bind('<Button-3>', right_menu)
def quick_key(self):
self.text_1.bind('<Control-C>', self.copytext_1)
self.text_1.bind('<Control-V>', self.getText_1)
self.text_2.bind('<Control-C>', self.copytext_2)
self.text_2.bind('<Control-V>', self.getText_2)
def translate(self):
content = self.text_1.get(1.0,END)
self.text_2.delete(1.0, END)
if content != '\n':
url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=baidu'
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)
req.add_header('User_Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36')
response=urllib.request.urlopen(req)
html=response.read().decode('utf-8')
target=json.loads(html)
for i in target['translateResult']:
for j in i:
self.text_2.insert(INSERT,j['tgt'])
def textclear(self):
self.text_1.delete(1.0, END)
self.text_2.delete(1.0, END)
if __name__ == '__main__':
Translations()
#其他 tkinter控件:
from Tkinter import *
root = Tk()
root.title("hello world")
root.geometry()
def print_item(event):
print lb.get(lb.curselection())
var = StringVar()
lb = Listbox(root, height=5, selectmode=BROWSE, listvariable = var)
lb.bind('<ButtonRelease-1>', print_item)
list_item = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
for item in list_item:
lb.insert(END, item)
scrl = Scrollbar(root)
scrl.pack(side=RIGHT, fill=Y)
lb.configure(yscrollcommand = scrl.set)
lb.pack(side=LEFT, fill=BOTH)
scrl['command'] = lb.yview
root.mainloop()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。