赞
踩
临时01
- import os
- from tkinter import filedialog
- import tkinter as tk
- from tkinter import messagebox
-
- root = tk.Tk()
- root.geometry('400x200+550+200')
- root.title('批量重命名文件小工具')
- page = tk.Frame()
- page.pack()
- text = tk.StringVar()
-
-
- def rename_file():
- filepath = filedialog.askdirectory()
- index = 0
- if len(os.listdir(filepath)) == 0:
- messagebox.showinfo(title="文件重命名", message="该目录下文件为空,请重新选择目录")
- else:
- for filename in os.listdir(filepath):
- index += 1
- file_path = os.path.join(filepath, filename)
- if os.path.isfile(file_path):
- name, ext = os.path.splitext(filename)
- new_name = text.get() + str(index) + ext
- # print(new_name)
- os.rename(file_path, os.path.join(filepath, new_name))
- messagebox.showinfo(title='文件重命名', message='文件重命名成功,请查看目录')
-
-
- tk.Label(page).grid(row=0, column=1)
- tk.Label(page, text='文件名称前缀:', font=('华文楷体', 15)).grid(row=2, column=1, pady=10)
- tk.Entry(page, textvariable=text).grid(row=2, column=2)
- tk.Button(page, text='选择目录并重命名文件', font=('华文楷体', 15), command=rename_file).grid(row=3, column=2)
- root.mainloop()
临时03
- import time
- import tkinter as tk
- from tkinter import messagebox
-
- root = tk.Tk()
- root.geometry('400x300+500+300')
- root.title('时间戳转换工具')
-
- usertime = tk.StringVar()
- usertimestamp = tk.StringVar()
-
- page = tk.Frame(root)
- page.pack()
-
- tk.Label(page).grid(row=0, column=1)
-
- tk.Label(page, text='请输入时间【格式:Y-M-D h:m:s】:', font=('黑体', 10)).grid(row=1, column=1, pady=10)
- tk.Entry(page, textvariable=usertime).grid(row=1, column=2)
-
- tk.Label(page, text='请输入时间戳: ', font=('黑体', 10)).grid(row=3, column=1, pady=30)
- tk.Entry(page, textvariable=usertimestamp).grid(row=3, column=2)
-
-
- # 将时间转换为时间戳,秒级
- def timestamp_s():
- time_value = usertime.get()
- timeArray = time.strptime(time_value, "%Y-%m-%d %H:%M:%S")
- timestamp = time.mktime(timeArray)
- times = int(timestamp)
- messagebox.showinfo(title='时间戳(s)', message=f'获取到的时间戳(s)为{times}')
-
-
- # 将时间转换为时间戳,毫秒级
- def timestamp_ms():
- time_value = usertime.get()
- timeArray = time.strptime(time_value, "%Y-%m-%d %H:%M:%S")
- timestamp = time.mktime(timeArray)
- times = int(round(timestamp * 1000))
- messagebox.showinfo(title='时间戳(s)', message=f'获取到的时间戳(s)为{times}')
-
-
- # 秒级时间戳转换
- def time_s():
- time_value = usertimestamp.get()
- if len(time_value) == 10:
- timeas = int(time_value)
- time_local = time.localtime(timeas)
- dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
- messagebox.showinfo(title='时间', message=f'获取到的时间为{dt}')
- elif len(time_value) == 13:
- timeas = int(time_value)
- timestamps = int(round(timeas / 1000))
- time_local = time.localtime(timestamps)
- dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
- messagebox.showinfo(title='时间', message=f'获取到的时间为{dt}')
-
- tk.Button(page, text='转换为时间戳(s)', command=timestamp_s).grid(row=2, column=1)
- tk.Button(page, text='转换为时间戳(ms)', command=timestamp_ms).grid(row=2, column=2)
- tk.Button(page, text='转换为时间', command=time_s).grid(row=4, column=1)
- root.mainloop()
临时02
- import tkinter as tk
- from tkinter import messagebox
-
-
- # 定义Book类
- class Book:
- def __init__(self, title, author, isbn):
- self.title = title
- self.author = author
- self.isbn = isbn
-
-
- # 定义GUI窗口
- class BookManagementSystem:
- def __init__(self, master):
- self.master = master
- self.master.title("图书管理系统")
-
- # 创建标题标签
- tk.Label(self.master, text="图书管理系统", font=("Arial", 20)).grid(column=0, row=0, columnspan=3, pady=10)
-
- # 创建书籍信息输入框
- tk.Label(self.master, text="书名").grid(column=0, row=1)
- self.title_entry = tk.Entry(self.master)
- self.title_entry.grid(column=1, row=1, padx=5, pady=5)
- tk.Label(self.master, text="作者").grid(column=0, row=2)
- self.author_entry = tk.Entry(self.master)
- self.author_entry.grid(column=1, row=2, padx=5, pady=5)
- tk.Label(self.master, text="ISBN号").grid(column=0, row=3)
- self.isbn_entry = tk.Entry(self.master)
- self.isbn_entry.grid(column=1, row=3, padx=5, pady=5)
-
- # 创建添加书籍按钮
- self.add_book_button = tk.Button(self.master, text="添加书籍", command=self.add_book)
- self.add_book_button.grid(column=0, row=4, pady=10)
-
- # 创建书籍列表框
- tk.Label(self.master, text="当前书籍列表").grid(column=2, row=1)
- self.book_listbox = tk.Listbox(self.master)
- self.book_listbox.grid(column=2, row=2, rowspan=3, padx=10, pady=5)
- self.update_book_list()
-
- # 创建删除书籍按钮
- self.delete_book_button = tk.Button(self.master, text="删除书籍", command=self.delete_book)
- self.delete_book_button.grid(column=2, row=4, pady=10)
-
- # 添加书籍方法
- def add_book(self):
- title = self.title_entry.get()
- author = self.author_entry.get()
- isbn = self.isbn_entry.get()
- book = Book(title, author, isbn)
- with open("books.txt", "a") as f:
- f.write(f"{book.title},{book.author},{book.isbn}\n")
- messagebox.showinfo("添加书籍", "添加书籍成功!")
- self.update_book_list()
-
- # 删除书籍方法
- def delete_book(self):
- selection = self.book_listbox.curselection()
- if len(selection) == 0:
- messagebox.showerror("删除书籍", "请选择要删除的书籍!")
- return
- index = selection[0]
- book = self.book_listbox.get(index)
- with open("books.txt", "r") as f:
- lines = f.readlines()
- with open("books.txt", "w") as f:
- for line in lines:
- if line.strip() != book:
- f.write(line)
- messagebox.showinfo("删除书籍", "删除书籍成功!")
- self.update_book_list()
-
- # 更新书籍列表方法
- def update_book_list(self):
- self.book_listbox.delete(0, tk.END)
- with open("books.txt", "r") as f:
- for line in f.readlines():
- book = line.strip()
- self.book_listbox.insert(tk.END, book)
-
-
- # 启动GUI窗口
- if __name__ == '__main__':
- root = tk.Tk()
- app = BookManagementSystem(root)
- root.mainloop()
临时04
- import tkinter as tk
- from tkinter import filedialog
- # PythonMargick包可以到Unofficial Windows Binaries for Python Extension Packages下载
- import PythonMagick
-
- root = tk.Tk()
- root.withdraw()
-
- Fpath = filedialog.askopenfilename()
-
- img = PythonMagick.Image(Fpath)
- # 这里要设置一下尺寸,不然会报ico尺寸异常错误
- img.sample('256x256')
- img.write('robin.ico')
临时05
- import base64
- import tkinter as tk
- from tkinter import filedialog
-
- root = tk.Tk()
- root.withdraw()
-
- Fpath = filedialog.askopenfilename()
- def file_to_base64():
- print(Fpath)
- f=open(Fpath,'rb') #二进制方式打开图文件
- ls_f=base64.b64encode(f.read()) #读取文件内容,转换为base64编码
- open('x.txt', 'wb').write(ls_f)
-
- def base64_to_file():
- print(Fpath)
- decoded = base64.b64decode(open(Fpath, 'rb').read())
- open('1.jpg', 'wb').write(decoded) # 保存
-
-
- if __name__ == '__main__':
- if "jpg" in Fpath or 'png' in Fpath or 'jpeg' in Fpath:
- file_to_base64()
- elif 'txt' in Fpath:
- base64_to_file()
requests模块请求第三方翻译接口,拿到数据后通过tkinter展示出来
临时06
- import requests
- import tkinter as tk
- from tkinter import messagebox
-
- root = tk.Tk()
- root.geometry('1000x500+350+100')
- root.title('英文翻译器')
- page = tk.Frame()
- page.pack()
-
- text = tk.StringVar()
- tk.Label(page).grid(row=0, column=1)
- tk.Label(page, text='翻译小程序', font=('黑体', 20)).grid(row=1, column=1, pady=20)
-
-
- def sure():
- page.pack_forget()
- page2.pack()
-
-
- def exit():
- page2.pack_forget()
- page.pack()
-
-
- def dcfy():
- url = 'https://fanyi.xxx.com/trans'
- headers = {
- "user-agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
- "referer": 'https://fanyi.xxx.com/'
- }
- chinese = text.get()
- data = {
- 'q': chinese,
- 'from': 'Auto',
- 'to': 'Auto'
- }
-
- resp = requests.post(url=url, headers=headers, data=data).json()
- chinatext = resp.get('web')[0]['key']
- Englishtext = resp.get('web')[0]['value'][0]
- messagebox.showinfo(title='单词翻译', message=Englishtext)
-
-
- def para():
- page.pack_forget()
- page3.pack()
-
-
- def exit2():
- page3.pack_forget()
- page.pack()
-
-
- def dlfy():
- url = 'https://fanyi.xxx.com/trans'
- headers = {
- "user-agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
- "referer": 'https://fanyi.xxx.com/'
- }
- chinese = text.get()
- data = {
- 'q': chinese,
- 'from': 'Auto',
- 'to': 'Auto'
- }
-
- resp = requests.post(url=url, headers=headers, data=data).json()
- Englishtext = resp['translation'][0]
- messagebox.showinfo(title='段落句子翻译', message=f'{Englishtext}')
-
-
- tk.Button(page, text='单词翻译', font=('黑体', 15), command=sure).grid(row=2, column=1, pady=20)
- tk.Button(page, text='句子翻译', font=('黑体', 15), command=para).grid(row=2, column=2, padx=10, pady=20)
-
- page2 = tk.Frame()
- tk.Entry(page2, textvariable=text, bd=5).grid(row=2, column=2, pady=20)
- tk.Button(page2, text='翻译', font=('黑体', 15), command=dcfy).grid(row=3, column=1, padx=20)
- tk.Button(page2, text='返回上一页', font=('黑体', 15), command=exit).grid(row=3, column=2, pady=20)
-
- page3 = tk.Frame()
- tk.Entry(page3, textvariable=text, bd=5).grid(row=2, column=2, pady=20)
- tk.Button(page3, text='翻译', font=('黑体', 15), command=dlfy).grid(row=3, column=1, padx=20)
- tk.Button(page3, text='返回上一页', font=('黑体', 15), command=exit2).grid(row=3, column=2, pady=20)
-
- root.mainloop()
临时07
- import tkinter as tk
- import glob
- from PIL import Image, ImageTk
-
- root = tk.Tk() # 创建窗口
- root.geometry('650x700+300+50') # 设置弹出窗口的大小和在屏幕中的位置
- root.title('图片查看器') # 设置弹出窗口的标题
-
- imgs = glob.glob('img/*.jpeg')
- imgs = [ImageTk.PhotoImage(Image.open(item)) for item in imgs]
-
- current_photo_no = 0
- img_label = tk.Label(root, image=imgs[current_photo_no], width=640, height=640)
- img_label.pack()
- number_var = tk.StringVar()
- number_var.set('1 of 8')
- tk.Label(root, textvariable=number_var, bd=1, relief=tk.SUNKEN, anchor=tk.CENTER).pack(fill=tk.X)
-
- button_form = tk.Frame(root)
- button_form.pack()
- prev_img = tk.Button(button_form, text='上一页')
- next_img = tk.Button(button_form, text='下一页')
- prev_img.pack(side=tk.LEFT, anchor=tk.CENTER)
- next_img.pack(side=tk.RIGHT, anchor=tk.CENTER)
-
-
- def change_images(next_no):
- global current_photo_no
- current_photo_no += next_no
- if current_photo_no >= len(imgs):
- current_photo_no = 0
- if current_photo_no < 0:
- current_photo_no = len(imgs) - 1
- number_var.set(f'{current_photo_no + 1} of {len(imgs)}')
- img_label.configure(image=imgs[current_photo_no])
-
-
- prev_img.config(command=lambda: change_images(-1))
- next_img.config(command=lambda: change_images(1))
- root.mainloop()
临时08
- import tkinter as tk
- from tkinter import messagebox
-
- root = tk.Tk()
- root.geometry('350x230+500+230') # 设置弹出框位置和大小
- # root.iconbitmap('E:/pythonProject/3.ico') # 设置弹出框图标
-
- root.title('BMI身体指数计算器')
-
- height = tk.DoubleVar()
- weight = tk.DoubleVar()
-
- page = tk.Frame(root)
- page.pack()
-
- tk.Label(page).grid(row=0, column=0)
-
- tk.Label(page, text='身高(米): ').grid(row=2, column=1, pady=20)
- tk.Entry(page, textvariable=height).grid(row=2, column=2)
-
- tk.Label(page, text='体重(kg): ').grid(row=3, column=1, pady=20)
- tk.Entry(page, textvariable=weight).grid(row=3, column=2)
-
-
- def jisuan():
- shengao = height.get()
- tizhong = weight.get()
- # print(shengao,tizhong)
- if shengao > 0 and tizhong > 0:
- BMI = tizhong / shengao ** 2
- BMI_new = float(('%.2f' % BMI))
- messagebox.showinfo(title='BMI身体指数计算',
- message=f'您的身高为{shengao}m,您的体重为{tizhong}kg,您的BMI身体指数为{BMI_new}')
- if BMI_new < 18.4:
- messagebox.showinfo(title='BMI身体指数计算', message='BMI指数较低,提示您的身体消瘦,要注意补充营养哦!')
- elif BMI_new > 18.5 and BMI_new < 24:
- messagebox.showinfo(title='BMI身体指数计算', message='BMI指数为正常值,继续加油!')
- elif BMI_new > 24 and BMI_new < 28:
- messagebox.showinfo(title='BMI身体指数计算',
- message='BMI指数较高,属于是超重了,提示您需要合理饮食,加强锻炼哦!')
- elif BMI_new > 28:
- messagebox.showinfo(title='BMI身体指数计算',
- message='BMI指数很高,属于肥胖了,提示您需要注意身体健康了,过胖会增加人体器官的负担哦!')
-
-
- tk.Button(page, text='计算', command=jisuan).grid(row=4, column=2, pady=10)
-
- root.mainloop()
-
小工具效果:对于json格式(混乱)的数据做规范修改,格式化输出json数据。
- import json
- import tkinter as tk
- from tkinter import filedialog
-
- root = tk.Tk()
- root.withdraw()
-
- json_data_path = filedialog.askopenfilename()
-
- with open(json_data_path) as json_file:
- data = json.load(json_file)
-
- formatted_data = json.dumps(data, indent=4, sort_keys=True)
- with open(json_data_path, 'w') as fp:
- fp.write(formatted_data)
代码逻辑:即通过python的pyautogui库定位到微信的像素位置,然后通过模拟鼠标键盘操作自动发送消息
实现步骤:
- import pyautogui
- import time
-
- def search_positon():
- time.sleep(1)
- x1,y1 = pyautogui.position()
- print('您当前的位置坐标为:','x:'+str(x1)+'','y:'+str(y1))
-
- if __name__ == '__main__':
- while True:
- search_positon()
- pass
效果展示:【具体方位不是固定的,只要确认可以点击打开微信即可】
- import pyautogui
- import time
- import pyperclip
-
- user = input('输入用户名称:')
- userinput = input('输入要发送的消息:')
- pyautogui.click(37, 428, button='left', clicks=2)
- # 找到微信程序位置,打开微信程序
- time.sleep(0.5)
- # 睡眠0.5秒
- pyautogui.click(368, 83, button='left', clicks=1)
- # 点击微信搜索框,具体位置可以通过运行上面的代码定位
- pyperclip.copy(user)
- pyautogui.hotkey('Ctrl', 'v')
- # 输入搜索内容
- pyautogui.press('enter')
- # 点击回车搜索
- time.sleep(0.5)
- pyautogui.press('enter')
- while True:
- pyperclip.copy(userinput)
- pyautogui.hotkey('Ctrl', 'v')
- pyautogui.press('enter')
- time.sleep(0.5)
- # 死循环执行消息发送,每隔0.5秒发送一次
效果展示:
临时09
- from PIL import Image
- import pytesseract
- import tkinter as tk
- from tkinter import filedialog
-
- root = tk.Tk()
- root.withdraw()
-
- Fpath = filedialog.askopenfilename()
-
-
- def read_image(name):
- with open('视频文件.txt','w',encoding='utf-8') as fp:
- fp.write(pytesseract.image_to_string(Image.open(name), lang='chi_sim'))
-
- def main():
- read_image(Fpath)
-
- if __name__ == '__main__':
- reslet = main()
-
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。