当前位置: 代码迷 >> 综合 >> python小项目图形化界面,翻译器,图片下载!
  详细解决方案

python小项目图形化界面,翻译器,图片下载!

热度:31   发布时间:2024-03-05 19:54:19.0

话不多说先上代码

import requests
import json
import tkinter as tk
import urllib
import re  #正则
from PIL import Image,ImageTk
from tkinter import ttk
import randomwin = tk.Tk()
win.geometry("700x400")win.title("检索器")ccccc = tk.Label(win,text="请输入词条")
ccccc.place(x=10, y=20, height=15)bbb = tk.Entry(win,width=15)
bbb.place(x=75, y=20, width=100, height=16)"""
词条"""
def aaa():shuru = str(bbb.get())url = "https://fanyi.baidu.com/sug""""UA伪装"""headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.68"}data = {"kw":shuru}"""发起请求"""response=requests.post(url=url,data=data,headers=headers)dic_obj=response.json()a = dic_obj.get("data")print("已成功获取网址信息")print("数据处理中...")for i in a:j=i.get("k")j1=i.get("v")f=j,j1ee.insert("end", f)ee.insert("end","\n")ee.insert("end", "\n")print("处理完成")button = tk.Button(win, text="词条检索", command=aaa)
button.place(x=195, y=17, width=100, height=25)
ee =tk.Text(win)
ee.place(x=10, y=70, width=325, height=300)"""背景"""global img0
photo = Image.open("u=2674208085,2485586514&fm=26&gp=0.jpg") #这个括号里是你的背景图片,如果不想设置背景可以直接删除 背景 代码块
photo = photo.resize((325, 300))
img0 = ImageTk.PhotoImage(photo)
img1 = ttk.Label(image=img0)
img1.place(x=350, y=70, width=325, height=300)"""图片
"""
def bbbbbb(j=0,Name="u=2674208085,2485586514&fm=26&gp=0.jpg"):shuru = str(bbb.get())# shuru1= urllib.parse.quote(shuru)headers={"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'}# # url = "https://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%E5%B0%8F%E7%8C%AB"## url1=url + shuru1url="https://image.baidu.com/search/index"params={"tn": "baiduimage","ipn": "r","ct": "201326592","cl": "2","lm": "-1","st": "-1","fm": "result","fr": "","sf": 1,"fmq": "1602587589377_R","pv": "","ic": "undefined","nc": "1","z": "0","hd": "undefined","latest": "undefined","copyright": "undefined","se": 1,"showtab": 0,"fb": 0,"width": "undefined","height": "undefined","face": 0,"istype": 2,"ie": "utf-8","ctd": "1602587589378^00_933X754","sid": "","word": shuru,}"""发起请求 获得页面"""html = requests.get(url=url,params=params,headers=headers)html.encoding='utf-8'html=html.text# Name=shuru+'.html'# with open(Name,'w',encoding="utf-8")as fp:#     fp.write(html)print("已成功获取图库网址信息")print("解析图库源码中...")"""通过正则  找到图片地址"""aaaaaa= '"objURL":"(.*?)",'dizhi = re.findall(aaaaaa,html,re.S)print("图片位置信息解析成功.")# print(dizhi)dizhi=dizhi[j]# print(dizhi)"""对图片地址发起请求"""pic=requests.get(dizhi,headers)name=shuru+'.jpg'print("图片获取中...")"""存储"""with open(name,"wb") as f:f.write(pic.content)print("图片获取成功")"""图片展示到图形化界面"""global img0print("图片处理中...")photo = Image.open(name)photo = photo.resize((325, 300))img0 = ImageTk.PhotoImage(photo)img1 = ttk.Label(image=img0)img1.place(x=350, y=70, width=325, height=300)# img11 = ttk.Button(tab1,text="选择照片",command = lambda : pick_photo(tab1))print("图片处理成功...")button = tk.Button(win, text="图片检索", command=bbbbbb)
button.place(x=325, y=17, width=100, height=25)"""同时检索"""
def tongshi():aaa()bbbbbb()button = tk.Button(win, text="同时检索", command=tongshi)
button.place(x=455, y=17, width=100, height=25)"""刷新板块"""def aaaaaaaa():c=random.randint(1, 10)bbbbbb(c)button = tk.Button(win, text="刷新", command=aaaaaaaa)
button.place(x=600, y=17, width=50, height=25)win.mainloop()

运行代码后

输入cat后单使用词条检索

再点击图片检索

点击刷新 更换新的图片

输入 pig 点击同时索引

PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取

python免费学习资料以及群交流解答点击即可加入