大家好,本文将围绕python turtle画图代码大全展开说明,python turtle画房子代码是一个很多人都想弄明白的事情,想搞清楚python turtle画生日蛋糕需要先了解以下几个事情。
大家好,小编来为大家解答以下问题,python编写小工具能解决什么重复性工作,python开发小工具项目,今天让我们一起来看看吧!
一、制作基于windows系统批量重命名文件小工具
参考博客:
使用python做一个批量重命名文件的小工具_讷言丶的博客-CSDN博客
效果展示:
临时01
代码实现:
import os
from tkinter import filedialog
import tkinter as tk
from tkinter import messagebox
root = ()
root.geometry('400x200+550+200')
root.title('批量重命名文件小工具')
page = tk.Frame()
()
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 = (filepath, filename)
if .isfile(file_path):
name, ext = .splitext(filename)
new_name = () + str(index) + ext
# print(new_name)
os.rename(file_path, (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 = ()
root.geometry('400x300+500+300')
root.title('时间戳转换工具')
usertime = tk.StringVar()
usertimestamp = tk.StringVar()
page = tk.Frame(root)
()
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 = ()
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 = ()
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 = ()
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
= 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)
(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)
(column=1, row=2, padx=5, pady=5)
tk.Label(self.master, text="ISBN号").grid(column=0, row=3)
_entry = tk.Entry(self.master)
(column=1, row=3, padx=5, pady=5)
# 创建添加书籍按钮
self.add_book_button = tk.Button(self.master, text="添加书籍", command=self.add_book)
(column=0, row=4, pady=10)
# 创建书籍列表框
tk.Label(self.master, text="当前书籍列表").grid(column=2, row=1)
self.book_listbox = tk.Listbox(self.master)
(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)
(column=2, row=4, pady=10)
# 添加书籍方法
def add_book(self):
title = ()
author = ()
isbn = ()
book = Book(title, author, isbn)
with open("", "a") as f:
f.write(f"{book.title},{book.author},{}\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 = (index)
with open("", "r") as f:
lines = f.readlines()
with open("", "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, )
with open("", "r") as f:
for line in f.readlines():
book = line.strip()
self.book_listbox.insert(, book)
# 启动GUI窗口
if __name__ == '__main__':
root = ()
app = BookManagementSystem(root)
root.mainloop()
四、制作jpg、png图片转ico图标小工具
效果展示:
临时04
代码实现:
import tkinter as tk
from tkinter import filedialog
# PythonMargick包可以到Unofficial Windows Binaries for Python Extension Packages下载
import PythonMagick
root = ()
root.withdraw()
Fpath = filedialog.askopenfilename()
img = PythonMagick.Image(Fpath)
# 这里要设置一下尺寸,不然会报ico尺寸异常错误
img.sample('256x256')
img.write('')
五、制作图片和base64互转小工具
效果展示:
临时05
代码实现:
import base64
import tkinter as tk
from tkinter import filedialog
root = ()
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 = ()
root.geometry('1000x500+350+100')
root.title('英文翻译器')
page = tk.Frame()
()
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():
_forget()
()
def exit():
_forget()
()
def dcfy():
url = ''
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": ''
}
chinese = ()
data = {
'q': chinese,
'from': 'Auto',
'to': 'Auto'
}
resp = (url=url, headers=headers, data=data).json()
chinatext = ('web')[0]['key']
Englishtext = ('web')[0]['value'][0]
messagebox.showinfo(title='单词翻译', message=Englishtext)
def para():
_forget()
()
def exit2():
_forget()
()
def dlfy():
url = ''
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": ''
}
chinese = ()
data = {
'q': chinese,
'from': 'Auto',
'to': 'Auto'
}
resp = (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 = () # 创建窗口
root.geometry('650x700+300+50') # 设置弹出窗口的大小和在屏幕中的位置
root.title('图片查看器') # 设置弹出窗口的标题
imgs = ('img/*.jpeg')
imgs = [ImageTk.PhotoImage((item)) for item in imgs]
current_photo_no = 0
img_label = tk.Label(root, image=imgs[current_photo_no], width=640, height=640)
()
number_var = tk.StringVar()
('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)
()
prev_img = tk.Button(button_form, text='上一页')
next_img = tk.Button(button_form, text='下一页')
(, anchor=tk.CENTER)
(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
(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()
八、制作BMI身体指数计算器(小工具):
效果展示:
临时08
代码实现:
import tkinter as tk
from tkinter import messagebox
root = ()
root.geometry('350x230+500+230') # 设置弹出框位置和大小
# root.iconbitmap('') # 设置弹出框图标
root.title('BMI身体指数计算器')
height = tk.DoubleVar()
weight = tk.DoubleVar()
page = tk.Frame(root)
()
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 = ()
tizhong = ()
# 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格式(混乱)的数据做规范修改,格式化输出json数据。
import json
import tkinter as tk
from tkinter import filedialog
root = ()
root.withdraw()
json_data_path = filedialog.askopenfilename()
with open(json_data_path) as json_file:
data = (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库定位到微信的像素位置,然后通过模拟鼠标键盘操作自动发送消息
实现步骤:
1)先通过pyautogui定位到微信的桌面位置怎么样用python绘制满天星。
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
效果展示:【具体方位不是固定的,只要确认可以点击打开微信即可】
2)编写消息发送逻辑,通过pyautogui自动下发微信消息
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)
# 点击微信搜索框,具体位置可以通过运行上面的代码定位
(user)
pyautogui.hotkey('Ctrl', 'v')
# 输入搜索内容
pyautogui.press('enter')
# 点击回车搜索
time.sleep(0.5)
pyautogui.press('enter')
while True:
(userinput)
pyautogui.hotkey('Ctrl', 'v')
pyautogui.press('enter')
time.sleep(0.5)
# 死循环执行消息发送,每隔0.5秒发送一次
效果展示:
十一,制作OCR图片识别小工具
参考博客:
【Python • 图片识别】pytesseract快速识别提取图片中的文字_python识别图片中的文字_广龙宇的博客-CSDN博客利用python做图片识别,识别提取图片中的文字会有很多方法,但是想要简单一点怎么办,那就可以使用tesseract识别引擎来实现,一行代码就可以做到提取图片文本。_python识别图片中的文字
效果展示:
临时09
代码实现:
from PIL import Image
import pytesseract
import tkinter as tk
from tkinter import filedialog
root = ()
root.withdraw()
Fpath = filedialog.askopenfilename()
def read_image(name):
with open('视频文件.txt','w',encoding='utf-8') as fp:
fp.write(pytesseract.image_to_string((name), lang='chi_sim'))
def main():
read_image(Fpath)
if __name__ == '__main__':
reslet = main()