2020-03-06百度翻译api

记录一下.小白而已.还有待进步.

简单讲一下,因为偶尔看到百度的api可以免费申请.

我就想随手写一个小儿简单的自己用用,后来,心血来潮,就想把平时用到的工具都放进去,

反正也没什么,就是ctrl+c 和v的事情.就有放进去了一个时间戳转换的.主要是方便使用,其他

想要什么功能也可以自己加进去,本来还想着再加一个redis用着,不过后来想想太鸡肋了,就放弃了,之后再想起来什么好用的可以添加.

前提是需要去百度开放平台申请appid跟密钥.

代码如下,记录和分享.

import tkinteras tk

import hashlib

import random

import requests

import time

import datetime

import win32clipboard

import win32con

import tkinter.messagebox

import re

import redis

# 主界面

def window():

root = tk.Tk()

root.title('工具盒')

root.geometry('750x500')

tk.Button(root, text="翻译", font=('bold', 14),  command=window_into, activebackground='DeepSkyBlue', width=8,

              height=4).grid(padx=15, pady=20)

tk.Button(root, text="时间戳", font=('bold', 14), command=timestamp, activebackground='DeepSkyBlue', width=8,

              height=4).grid(row=0, column=2)

# tk.Button(root, text="redis", font=('bold', 14), command=, activebackground='DeepSkyBlue', width=8,

#          height=4).grid(row=0, column=3, padx=15)

    root.mainloop()

# 百度翻译

def window_into():

root1 = tk.Tk()

root1.title('小翻译')

root1.geometry('750x500')

def fanyi_api():

app_key ='appkey'

        app_secret ='app密钥'

        q = info.get('0.0', 'end').replace('\n', '~')

salt =str(random.randint(0000000000, 9999999999))

sign = hashlib.md5((app_key + q + salt + app_secret).encode(encoding='UTF-8')).hexdigest()

req_url ='http://api.fanyi.baidu.com/api/trans/vip/translate?' +'q=' + q +'&from=auto&to=auto&appid=' + \

app_key +'&salt=' + salt +'&sign=' + sign

r = requests.post(req_url)

if len(list(r.json())) >=3:

fanyi_re = r.json()['trans_result'][0]['dst'].replace('~', '\n').replace(' ', '')

into.delete(1.0, "end")

into.insert('end', fanyi_re)

else:

error_code = r.json()['error_code']

error_message = r.json()['error_msg']

into.delete(1.0, "end")

into.insert('end', (error_code, error_message))

into = tk.Text(root1, fg='Black', font=('bold', 12), width=82, height=24)

def changered(event):

got = info.get('1.0', '1.9')

if got =='请输入内容':

info.delete(0.0, 'end')

info.insert(0.0, "")

info = tk.Text(root1, fg='Black', font=('bold', 12))

info.insert(0.0, '请输入内容')

info.bind('', changered)

info.place(x=15, y=12, width=660, height=80)

tk.Button(root1, text="翻译", bd='1', font=('bold', 14), command=fanyi_api, width=4, height=1).grid(padx=690, pady=12)

into.grid(stick=tk.SW, padx=15, pady=45)

root1.mainloop()

# 时间戳

def timestamp():

timec = tk.Tk()

timec.title('时间戳')

timec.geometry('750x500')

def uptime():

first_lable.config(text=('当前时间:%s' % datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))

timec.after(100, uptime)

def send_to_clipboard():

win32clipboard.OpenClipboard()

win32clipboard.EmptyClipboard()

new_time = first_lable.cget('text')

new_time1 = re.findall(r":(.+)", new_time)[0]

win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT, new_time1)

win32clipboard.CloseClipboard()

def stamp_transform():

into_time.delete(0.0, 'end')

original = info_stamp.get('0.0', 'end').replace('\n', '')

if len(original) >13:

tkinter.messagebox.showinfo('提示', '超出输入长度\n最多输入13位')

elif len(original) ==0:

tkinter.messagebox.showinfo('提示', '不能为空')

info_judge = re.search(r'\D', original)

if info_judgeis None:

info_text =int(original)

if len(str(info_text)) <13:

timearray = time.localtime(info_text)

otherstyletime = time.strftime("%Y-%m-%d %H:%M:%S", timearray)

into_time.insert(0.0, otherstyletime)

else:

timearray = time.localtime(info_text/1000)

otherstyletime = time.strftime("%Y-%m-%d %H:%M:%S", timearray)

into_time.insert(0.0, otherstyletime)

else:

tkinter.messagebox.showinfo('提示', '只能输入数字')

def ten_time_transform():

ten_into_stamp.delete(0.0, 'end')

present_time = info_time.get('0.0', 'end').replace('\n', '')

if len(present_time) ==0:

tkinter.messagebox.showinfo('提示', '不能为空')

format_time = time.strptime(present_time, "%Y-%m-%d %H:%M:%S")

transform_result =int(time.mktime(format_time))

ten_into_stamp.insert(0.0, transform_result)

def thirteen_time_transform():

thirteen_into_stamp.delete(0.0, 'end')

present_time1 = info_time.get('0.0', 'end').replace('\n', '')

if len(present_time1) ==0:

tkinter.messagebox.showinfo('提示', '不能为空')

format_time1 = time.strptime(present_time1, "%Y-%m-%d %H:%M:%S")

transform_result1 =int(time.mktime(format_time1) *1000)

print(transform_result1)

thirteen_into_stamp.insert(0.0, transform_result1)

# 第一行

    time_now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

first_lable = tk.Label(timec, text=('当前时间:%s' % time_now), font=('Black', 12), height=2, width=35)

first_lable.grid()

copy_time = tk.Button(timec, bd=1, height=1, width=5, text="复制", command=send_to_clipboard).grid(row=0, column=2)

# 第二行

    second_lable = tk.Label(timec, text='时间戳→时间', font=('Black', 12), height=1, width=13).grid(row=1)

# 第三行

    info_stamp = tk.Text(timec, font=16, height=1, width=30)

info_stamp.grid(row=2, padx=10, pady=1)

stamp_time = tk.Button(timec, bd=1, height=1, width=10, text="转换→", command=stamp_transform).grid(row=2, column=2)

into_time = tk.Text(timec, font=16, height=1, width=30)

into_time.grid(row=2, column=3, padx=5)

# 第四行

    thirdly_lable = tk.Label(timec, text='时间→时间戳', font=('Black', 12), height=1, width=13).grid(row=4)

# 第五行

    tk.Button(timec, bd=1, height=1, width=10, text="转换10位→", command=ten_time_transform).grid(row=5, column=2)

ten_into_stamp = tk.Text(timec, font=16, height=1, width=30)

ten_into_stamp.grid(row=5, column=3, padx=5)

# 第六行

    info_time = tk.Text(timec, font=16, height=1, width=30)

info_time.grid(row=6, padx=10, pady=1)

# 第七行

    tk.Button(timec, bd=1, height=1, width=10, text="转换13位→", command=thirteen_time_transform).grid(row=7, column=2)

thirteen_into_stamp = tk.Text(timec, font=16, height=1, width=30)

thirteen_into_stamp.grid(row=7, column=3, padx=5)

# 更新

    timec.after(100, uptime)

timec.mainloop()

你可能感兴趣的:(2020-03-06百度翻译api)