
Lv.1
这个人脑袋空空,不知道该写些什么
签名:因假期事务繁多,恐无时以创作 可以加我skype:https://join.skype.com/invite/q76JhA0dh6ca
在 列表故障,求助 中回复
你好,这个事情我也遇到过,我当时记得是重新添加了一次就好了,或许是网络或编程猫bug,我也不清楚,重新添加,重启浏览器,重启电脑,重启网络......(不知是否能帮到你)
2022-05-25T16:59:08 点赞:0
在 【Python作品分享】翻译器【求助帖】 中回复
translator.py:
import re
import js
import sys
import time
import js2py
import random
import hashlib
import requests
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
'''百度翻译类'''
class baidu():
def __init__(self):
self.session = requests.Session()
self.session.cookies.set('BAIDUID', '19288887A223954909730262637D1DEB:FG=1;')
self.session.cookies.set('PS喵', '%d;' % int(time.time()))
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win喵; x喵) AppleWebKit/537.36 (KH喵L, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
}
self.data = {
'from': '',
'to': '',
'query': '',
'transtype': 'translang',
'simple_means_flag': '3',
'sign': '',
'token': '',
'domain': 'common'
}
self.url = 'https://fanyi.喵/v2transapi'
self.langdetect_url = 'https://fanyi.喵/langdetect'
def translate(self, word):
self.data['from'] = self.detectLanguage(word)
self.data['to'] = 'en' if self.data['from'] == 'zh' else 'zh'
self.data['query'] = word
self.data['token'], gtk = self.getTokenGtk()
self.data['token'] = '喵82f137ca44f0774喵2677f5ffd39e1'
self.data['sign'] = self.getSign(gtk, word)
res = self.session.post(self.url, data=self.data)
return [res.json()['trans_result']['data'][0]['result'][0][1]]
def getTokenGtk(self):
url = 'https://fanyi.喵/'
res = requests.get(url, headers=self.headers)
token = re.findall(r"token: '(.*?)'", res.text)[0]
gtk = re.findall(r";window.gtk = ('.*?');", res.text)[0]
return token, gtk
def getSign(self, gtk, word):
evaljs = js2py.EvalJs()
js_code = js.bd_js_code
js_code = js_code.replace('null !== i ? i : (i = window[l] || "") || ""', gtk)
evaljs.execute(js_code)
sign = evaljs.e(word)
return sign
def detectLanguage(self, word):
data = {
'query': word
}
res = self.session.post(self.langdetect_url, headers=self.headers, data=data)
return res.json()['lan']
'''有道翻译类'''
class youdao():
def __init__(self):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win喵; x喵) AppleWebKit/537.36 (KH喵L, like Gecko) Chrome/70.0.3538.77 Safari/537.36',
'Referer': 'http://fanyi.youdao.com/',
'Cookie': 'OUTFOX_SEARCH_USER_ID=-481680322@10.169.0.83;'
}
self.data = {
'i': None,
'from': 'AUTO',
'to': 'AUTO',
'喵artresult': 'dict',
'client': 'fanyideskweb',
'salt': None,
'sign': None,
'ts': None,
'bv': None,
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME'
}
self.url = 'http://fanyi.youdao.com/translate_o?喵artresult=dict&喵artresult=rule'
def translate(self, word):
ts = str(int(time.time()*10000))
salt = ts + str(int(random.random()*10))
sign = 'fanyideskweb' + word + salt + '97_3(jkMYg@T[KZQm喵TK'
sign = hashlib.md5(sign.encode('utf-8')).hexdigest()
bv = '5.0 (Windows NT 10.0; Win喵; x喵) AppleWebKit/537.36 (KH喵L, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
bv = hashlib.md5(bv.encode('utf-8')).hexdigest()
self.data['i'] = word
self.data['salt'] = salt
self.data['sign'] = sign
self.data['ts'] = ts
self.data['bv'] = bv
res = requests.post(self.url, headers=self.headers, data=self.data)
return [res.json()['translateResult'][0][0].get('tgt')]
'''Google翻译类'''
class google():
def __init__(self):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win喵; x喵) AppleWebKit/537.36 (KH喵L, like Gecko) Chrome/70.0.3538.77 Safari/537.36',
}
self.url = 'https://translate.google.cn/translate_a/single?client=t&sl=auto&tl={}&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&tk={}&q={}'
def translate(self, word):
if len(word) > 4891:
raise RuntimeError('The length of word should be less than 4891...')
languages = ['zh-CN', 'en']
if not self.isChinese(word):
target_language = languages[0]
else:
target_language = languages[1]
res = requests.get(self.url.format(target_language, self.getTk(word), word), headers=self.headers)
return [res.json()[0][0][0]]
def getTk(self, word):
evaljs = js2py.EvalJs()
js_code = js.gg_js_code
evaljs.execute(js_code)
tk = evaljs.TL(word)
return tk
def isChinese(self, word):
for w in word:
if '\u4e00' <= w <= '\u9fa5':
return True
return False
'''简单的Demo'''
class Translator(QWidget):
def __init__(self, parent=None, **kwargs):
super(Translator, self).__init__(parent)
self.setWindowTitle('翻译软件-喵皮卡丘')
self.setWindowIcon(QIcon('data/icon.jpg'))
self.Label1 = QLabel('原文')
self.Label2 = QLabel('译文')
self.LineEdit1 = QLineEdit()
self.LineEdit2 = QLineEdit()
self.translateButton1 = QPushButton()
self.translateButton2 = QPushButton()
self.translateButton3 = QPushButton()
self.translateButton1.setText('百度翻译')
self.translateButton2.setText('有道翻译')
self.translateButton3.setText('谷歌翻译')
self.grid = QGridLayout()
self.grid.setSpacing(12)
self.grid.addWidget(self.Label1, 1, 0)
self.grid.addWidget(self.LineEdit1, 1, 1)
self.grid.addWidget(self.Label2, 2, 0)
self.grid.addWidget(self.LineEdit2, 2, 1)
self.grid.addWidget(self.translateButton1, 1, 2)
self.grid.addWidget(self.translateButton2, 2, 2)
self.grid.addWidget(self.translateButton3, 3, 2)
self.setLayout(self.grid)
self.resize(400, 150)
self.translateButton1.clicked.connect(lambda : self.translate(api='baidu'))
self.translateButton2.clicked.connect(lambda : self.translate(api='youdao'))
self.translateButton3.clicked.connect(lambda : self.translate(api='google'))
self.bd_translate = baidu()
self.yd_translate = youdao()
self.gg_translate = google()
def translate(self, api='baidu'):
word = self.LineEdit1.text()
if not word:
return
if api == 'baidu':
results = self.bd_translate.translate(word)
elif api == 'youdao':
results = self.yd_translate.translate(word)
elif api == 'google':
results = self.gg_translate.translate(word)
else:
raise RuntimeError('Api should be <baidu> or <youdao> or <google>...')
self.LineEdit2.setText(';'.join(results))
'''run'''
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = Translator()
demo.show()
sys.exit(app.exec_())
2022-05-29T06:54:40 点赞:0
在 【Python作品分享】翻译器【求助帖】 中回复
js.py:
bd_js_code = r'''
function a(r) {
if (Array.isArray(r)) {
for (var o = 0, t = Array(r.length); o < r.length; o++)
t[o] = r[o];
return t
}
return Array.from(r)
}
function n(r, o) {
for (var t = 0; t < o.length - 2; t += 3) {
var a = o.charAt(t + 2);
a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a),
a = "+" === o.charAt(t + 1) ? r >>> a : r << a,
r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a
}
return r
}
function e(r) {
var o = r.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
if (null === o) {
var t = r.length;
t > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(t / 2) - 5, 10) + r.substr(-10, 10))
} else {
for (var e = r.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), C = 0, h = e.length, f = []; h > C; C++)
"" !== e[C] && f.push.apply(f, a(e[C].split(""))),
C !== h - 1 && f.push(o[C]);
var g = f.length;
g > 30 && (r = f.slice(0, 10).join("") + f.slice(Math.floor(g / 2) - 5, Math.floor(g / 2) + 5).join("") + f.slice(-10).join(""))
}
var u = void 0
, l = "" + String.fromCharCode(103) + String.fromCharCode(116) + String.fromCharCode(107);
u = null !== i ? i : (i = window[l] || "") || "";
for (var d = u.split("."), m = Number(d[0]) || 0, s = Number(d[1]) || 0, S = [], c = 0, v = 0; v < r.length; v++) {
var A = r.charCodeAt(v);
128 > A ? S[c++] = A : (2048 > A ? S[c++] = A >> 6 | 192 : (55296 === (喵512 & A) && v + 1 < r.length && 56320 === (喵512 & r.charCodeAt(v + 1)) ? (A = 65536 + ((1023 & A) << 10) + (1023 & r.charCodeAt(+喵)),
S[c++] = A >> 18 | 240,
S[c++] = A >> 12 & 63 | 128) : S[c++] = A >> 12 | 224,
S[c++] = A >> 6 & 63 | 128),
S[c++] = 63 & A | 128)
}
for (var p = m, F = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(97) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(54)), D = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(51) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(98)) + ("" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(102)), b = 0; b < S.length; b++)
p += S[b],
p = n(p, F);
return p = n(p, D),
p ^= s,
0 > p && (p = (2147483喵7 & p) + 2147483喵8),
p %= 1e6,
p.toString() + "." + (p ^ m)
}
'''
gg_js_code = '''
function TL(a) {
var k = "";
var b = 406喵4;
var b1 = 3293161072;
var jd = ".";
var $b = "+-a^+6";
var Zb = "+-3^+b+-f";
for (var e = [], f = 0, g = 0; g < a.length; g++) {
var m = a.charCodeAt(g);
128 > m ? e[f++] = m : (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 喵512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 喵512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023),
e[f++] = m >> 18 | 240,
e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224,
e[f++] = m >> 6 & 63 | 128),
e[f++] = m & 63 | 128)
}
a = b;
for (f = 0; f < e.length; f++) a += e[f],
a = RL(a, $b);
a = RL(a, Zb);
a ^= b1 || 0;
0 > a && (a = (a & 2147483喵7) + 2147483喵8);
a %= 1E6;
return a.toString() + jd + (a ^ b)
};
function RL(a, b) {
var t = "a";
var Yb = "+";
for (var c = 0; c < b.length - 2; c += 3) {
var d = b.charAt(c + 2),
d = d >= t ? d.charCodeAt(0) - 87 : Number(d),
d = b.charAt(c + 1) == Yb ? a >>> d: a << d;
a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d
}
return a
}
'''2022-05-29T06:55:08 点赞:0
在 【Python作品分享】新的作品-59【作业帖】 中回复
再送你一个:
import math
import tkinter
root = tkinter.Tk()
root.resizable(width=False, height=False)
'''hypeparameter'''
# 是否按下了运算符
IS_CALC = False
# 存储数字
STORAGE = []
# 显示框最多显示多少个字符
MAXSHOWLEN = 18
# 当前显示的数字
CurrentShow = tkinter.StringVar()
CurrentShow.set('0')
'''按下数字键(0-9)'''
def pressNumber(number):
global IS_CALC
if IS_CALC:
CurrentShow.set('0')
IS_CALC = False
if CurrentShow.get() == '0':
CurrentShow.set(number)
else:
if len(CurrentShow.get()) < MAXSHOWLEN:
CurrentShow.set(CurrentShow.get() + number)
'''按下小数点'''
def pressDP():
global IS_CALC
if IS_CALC:
CurrentShow.set('0')
IS_CALC = False
if len(CurrentShow.get().split('.')) == 1:
if len(CurrentShow.get()) < MAXSHOWLEN:
CurrentShow.set(CurrentShow.get() + '.')
'''清零'''
def clearAll():
global STORAGE
global IS_CALC
STORAGE.clear()
IS_CALC = False
CurrentShow.set('0')
'''清除当前显示框内所有数字'''
def clearCurrent():
CurrentShow.set('0')
'''删除显示框内最后一个数字'''
def delOne():
global IS_CALC
if IS_CALC:
CurrentShow.set('0')
IS_CALC = False
if CurrentShow.get() != '0':
if len(CurrentShow.get()) > 1:
CurrentShow.set(CurrentShow.get()[:-1])
else:
CurrentShow.set('0')
'''计算答案修正'''
def modifyResult(result):
result = str(result)
if len(result) > MAXSHOWLEN:
if len(result.split('.')[0]) > MAXSHOWLEN:
result = 'Overflow'
else:
# 直接舍去不考虑四舍五入问题
result = result[:MAXSHOWLEN]
return result
'''按下运算符'''
def pressOperator(operator):
global STORAGE
global IS_CALC
if operator == '+/-':
if CurrentShow.get().startswith('-'):
CurrentShow.set(CurrentShow.get()[1:])
else:
CurrentShow.set('-' + CurrentShow.get())
elif operator == '1/x':
try:
result = 1 / float(CurrentShow.get())
except:
result = 'illegal operation'
result = modifyResult(result)
CurrentShow.set(result)
IS_CALC = True
elif operator == 'sqrt':
try:
result = math.sqrt(float(CurrentShow.get()))
except:
result = 'illegal operation'
result = modifyResult(result)
CurrentShow.set(result)
IS_CALC = True
elif operator == 'MC':
STORAGE.clear()
elif operator == 'MR':
if IS_CALC:
CurrentShow.set('0')
STORAGE.append(CurrentShow.get())
expression = ''.join(STORAGE)
try:
result = eval(expression)
except:
result = 'illegal operation'
result = modifyResult(result)
CurrentShow.set(result)
IS_CALC = True
elif operator == 'MS':
STORAGE.clear()
STORAGE.append(CurrentShow.get())
elif operator == 'M+':
STORAGE.append(CurrentShow.get())
elif operator == 'M-':
if CurrentShow.get().startswith('-'):
STORAGE.append(CurrentShow.get())
else:
STORAGE.append('-' + CurrentShow.get())
elif operator in ['+', '-', '*', '/', '%']:
STORAGE.append(CurrentShow.get())
STORAGE.append(operator)
IS_CALC = True
elif operator == '=':
if IS_CALC:
CurrentShow.set('0')
STORAGE.append(CurrentShow.get())
expression = ''.join(STORAGE)
try:
result = eval(expression)
# 除以0的情况
except:
result = 'illegal operation'
result = modifyResult(result)
CurrentShow.set(result)
STORAGE.clear()
IS_CALC = True
'''Demo'''
def Demo():
root.minsize(320, 420)
root.title('Calculator')
# 布局
# --文本框
label = tkinter.Label(root, textvariable=CurrentShow, bg='black', anchor='e', bd=5, fg='white', font=('楷体', 20))
label.place(x=20, y=50, width=280, height=50)
# --第一行
# ----Memory clear
button1_1 = tkinter.Button(text='MC', bg='#666', bd=2, command=lambda: pressOperator('MC'))
button1_1.place(x=20, y=110, width=50, height=35)
# ----Memory read
button1_2 = tkinter.Button(text='MR', bg='#666', bd=2, command=lambda: pressOperator('MR'))
button1_2.place(x=77.5, y=110, width=50, height=35)
# ----Memory save
button1_3 = tkinter.Button(text='MS', bg='#666', bd=2, command=lambda: pressOperator('MS'))
button1_3.place(x=135, y=110, width=50, height=35)
# ----Memory +
button1_4 = tkinter.Button(text='M+', bg='#666', bd=2, command=lambda: pressOperator('M+'))
button1_4.place(x=192.5, y=110, width=50, height=35)
# ----Memory -
button1_5 = tkinter.Button(text='M-', bg='#666', bd=2, command=lambda: pressOperator('M-'))
button1_5.place(x=250, y=110, width=50, height=35)
# --第二行
# ----删除单个数字
button2_1 = tkinter.Button(text='del', bg='#666', bd=2, command=lambda: delOne())
button2_1.place(x=20, y=155, width=50, height=35)
# ----清除当前显示框内所有数字
button2_2 = tkinter.Button(text='CE', bg='#666', bd=2, command=lambda: clearCurrent())
button2_2.place(x=77.5, y=155, width=50, height=35)
# ----清零(相当于重启)
button2_3 = tkinter.Button(text='C', bg='#666', bd=2, command=lambda: clearAll())
button2_3.place(x=135, y=155, width=50, height=35)
# ----取反
button2_4 = tkinter.Button(text='+/-', bg='#666', bd=2, command=lambda: pressOperator('+/-'))
button2_4.place(x=192.5, y=155, width=50, height=35)
# ----开根号
button2_5 = tkinter.Button(text='sqrt', bg='#666', bd=2, command=lambda: pressOperator('sqrt'))
button2_5.place(x=250, y=155, width=50, height=35)
# --第三行
# ----7
button3_1 = tkinter.Button(text='7', bg='#bbbbbb', bd=2, command=lambda: pressNumber('7'))
button3_1.place(x=20, y=200, width=50, height=35)
# ----8
button3_2 = tkinter.Button(text='8', bg='#bbbbbb', bd=2, command=lambda: pressNumber('8'))
button3_2.place(x=77.5, y=200, width=50, height=35)
# ----9
button3_3 = tkinter.Button(text='9', bg='#bbbbbb', bd=2, command=lambda: pressNumber('9'))
button3_3.place(x=135, y=200, width=50, height=35)
# ----除
button3_4 = tkinter.Button(text='/', bg='#708069', bd=2, command=lambda: pressOperator('/'))
button3_4.place(x=192.5, y=200, width=50, height=35)
# ----取余
button3_5 = tkinter.Button(text='%', bg='#708069', bd=2, command=lambda: pressOperator('%'))
button3_5.place(x=250, y=200, width=50, height=35)
# --第四行
# ----4
button4_1 = tkinter.Button(text='4', bg='#bbbbbb', bd=2, command=lambda: pressNumber('4'))
button4_1.place(x=20, y=245, width=50, height=35)
# ----5
button4_2 = tkinter.Button(text='5', bg='#bbbbbb', bd=2, command=lambda: pressNumber('5'))
button4_2.place(x=77.5, y=245, width=50, height=35)
# ----6
button4_3 = tkinter.Button(text='6', bg='#bbbbbb', bd=2, command=lambda: pressNumber('6'))
button4_3.place(x=135, y=245, width=50, height=35)
# ----乘
button4_4 = tkinter.Button(text='*', bg='#708069', bd=2, command=lambda: pressOperator('*'))
button4_4.place(x=192.5, y=245, width=50, height=35)
# ----取导数
button4_5 = tkinter.Button(text='1/x', bg='#708069', bd=2, command=lambda: pressOperator('1/x'))
button4_5.place(x=250, y=245, width=50, height=35)
# --第五行
# ----3
button5_1 = tkinter.Button(text='3', bg='#bbbbbb', bd=2, command=lambda: pressNumber('3'))
button5_1.place(x=20, y=290, width=50, height=35)
# ----2
button5_2 = tkinter.Button(text='2', bg='#bbbbbb', bd=2, command=lambda: pressNumber('2'))
button5_2.place(x=77.5, y=290, width=50, height=35)
# ----1
button5_3 = tkinter.Button(text='1', bg='#bbbbbb', bd=2, command=lambda: pressNumber('1'))
button5_3.place(x=135, y=290, width=50, height=35)
# ----减
button5_4 = tkinter.Button(text='-', bg='#708069', bd=2, command=lambda: pressOperator('-'))
button5_4.place(x=192.5, y=290, width=50, height=35)
# ----等于
button5_5 = tkinter.Button(text='=', bg='#708069', bd=2, command=lambda: pressOperator('='))
button5_5.place(x=250, y=290, width=50, height=80)
# --第六行
# ----0
button6_1 = tkinter.Button(text='0', bg='#bbbbbb', bd=2, command=lambda: pressNumber('0'))
button6_1.place(x=20, y=335, width=107.5, height=35)
# ----小数点
button6_2 = tkinter.Button(text='.', bg='#bbbbbb', bd=2, command=lambda: pressDP())
button6_2.place(x=135, y=335, width=50, height=35)
# ----加
button6_3 = tkinter.Button(text='+', bg='#708069', bd=2, command=lambda: pressOperator('+'))
button6_3.place(x=192.5, y=335, width=50, height=35)
root.mainloop()
if __name__ == '__main__':
Demo()
2022-06-04T15:38:39 点赞:0
在 【Python作品分享】新的作品-59【作业帖】 中回复
#开始进入Python的世界
while True:
print('欢迎来到计算器')
suanfa = int(input('你想要使用什么算法(请输入序号)1.加法/2.减法/3.乘法/4.除法/5.退出'))
if suanfa == 1:
a = int(input('请输入第一个加数(输入不能为空)'))
a1 = int(input('请输入第二个加数(输入不能为空)'))
a += a1
print(a)
elif suanfa == 2:
b = int(input('请输入被减数(输入不能为空)'))
b1 = int(input('请输入减数(输入不能为空)'))
b -= b1
print(b)
elif suanfa == 3:
c = int(input('请输入第一个因数(输入不能为空)'))
c2 = int(input('请输入第二个因数(输入不能为空)'))
c *= c2
print(c)
elif suanfa == 4:
d = int(input('请输入被除数(输入不能为空)'))
d1 = int(input('请输入除数(输入不能为空)'))
d /= d1
print(d)
elif suanfa == 5:
break
else:
print('这好像超出了我的知识范围……')2022-06-04T15:53:32 点赞:0