今天笔者结识的朋友,询问关于如何快速编写一个exe工具的问题。由于功能简单且之前无相关GUI编程基础,为了快速完成开发,我向他推荐了easygui模块。
python作为胶水语言,几乎没有不能做的事情,但个人一直觉得在GUI开发方面python可以算作是短板了,为什么?因为性能…python的性能问题,往往出现在其他编程语言对其的鄙夷中。但不管如何python在GUI编程上,也是有大量优秀模块的
tkinter
也许有些人不知道这个模块,但如果你安装python后,使用过自带的IDLE,那么你就应该了解它。tkinter作为python自带的GUI模块,IDLE便是通过该模块开发的,也是我比较喜欢的一个模块
easygui
第一次了解这个模块,还是在小甲鱼的《零基础入门学习python》时学习的,该模块对Tkinter进行了二次封装,是的GUI操作变得更为简单,但同时也局限于表层的使用,
wxpython
wxpython是一个Python包装wxWidgets(这是用 C++ 编写),一个流行的跨平台GUI工具包。在wxPython API主要模块包括一个核心模块。
wxpython的优势在速运行速度较快,但编写起来较为复杂,而且界面美化效果较差…
PYQT5
Qt是一套跨平台的C++库,不论是C、Java还是Python,都可以通过它来实现GUI界面的开发。PyQt5是Qt v5的一套全面的Python绑定。它实现为超过35个扩展模块,并使Python能够在所有支持的平台(包括iOS和Android)上用作C ++的替代应用程序开发语言。
如果你对GUI编程非常感谢兴趣,可以用心学学这个模块,但如果你只是想快速的实现一些GUI的功能,我更推荐大家学习tkinter模块,为什么?
- tkinter作为python默认自带的模块库,无需单独安装
- tkinter相比于wxpython与pyqt在针对小工具的编程方面,更为高效
- tkinter在网上存在大量的快速入门教程
tkinter实战
为了证明tkinter的方便与高效,我将很早前在公司写的一个pythonGUI代码,将图片转化为素描画的小工具,晚上重写了一次,先给大家看看实现效果:
避坑指南
tktiner添加图片
大家看到,tktiner中附带了一个我的微信二维码,这里只是为了演示tkinter添加图片,没有丝毫推广的味道,哈哈…这里要注意下,tktiner在添加图片时,PhotoImage(file=’tmp.gif’)要求图片必须是.gif结尾的图片,否则就会报错:
_tkinter.TclError: couldn’t recognize data in image file
关于base64
大家看到我没有加载图片,而是通过base64提前转码好二进制文件后,再进行导入,这样我们打包的exe在使用时,就无需附带一个图片文件了!
import base64
with open('demo.gif','rb') as f:
data = f.read()
img = base64.b64encode(data)
tkinter打包
既然我们开发的是GUI工具,必须得打包成exe才更好玩啊!但是以往很正常的打包,这次居然报错了:
找了半天才发现,从python 3.6开始,enum34库不再与标准库兼容。该库也是不必要的,因此只需卸载它即可。pip uninstall enum34
再次执行,大功告成!
总体代码
最后附上工具的整体代码,可供大家参考:
# -*- coding: utf-8 -*-
# @Author : 王翔
# @JianShu : 清风Python
# @Date : 2019/8/6 01:18
# @Software : PyCharm
# @version :Python 3.7.3
# @File : ChangeImage.py
from tkinter.messagebox import *
from tkinter.filedialog import *
from tkinter import *
import base64
from PIL import Image, ImageFilter, ImageOps
import os
img_bs64 = b'R0lGODlhrACsAHAAACH5BAEAAPwALAAAAACsAKwAhwAAAAAAMwAAZgAAmQAAzAAA/wArAAArMwArZgArmQArzAAr/wBVAABVMwBVZgBVmQBVzABV/wCAAACAMwCAZgCAmQCAzACA/wCqAACqMwCqZgCqmQCqzACq/wDVAADVMwDVZgDVmQDVzADV/wD/AAD/MwD/ZgD/mQD/zAD//zMAADMAMzMAZjMAmTMAzDMA/zMrADMrMzMrZjMrmTMrzDMr/zNVADNVMzNVZjNVmTNVzDNV/zOAADOAMzOAZjOAmTOAzDOA/zOqADOqMzOqZjOqmTOqzDOq/zPVADPVMzPVZjPVmTPVzDPV/zP/ADP/MzP/ZjP/mTP/zDP//2YAAGYAM2YAZmYAmWYAzGYA/2YrAGYrM2YrZmYrmWYrzGYr/2ZVAGZVM2ZVZmZVmWZVzGZV/2aAAGaAM2aAZmaAmWaAzGaA/2aqAGaqM2aqZmaqmWaqzGaq/2bVAGbVM2bVZmbVmWbVzGbV/2b/AGb/M2b/Zmb/mWb/zGb//5kAAJkAM5kAZpkAmZkAzJkA/5krAJkrM5krZpkrmZkrzJkr/5lVAJlVM5lVZplVmZlVzJlV/5mAAJmAM5mAZpmAmZmAzJmA/5mqAJmqM5mqZpmqmZm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1jXlZllwpiTu2knEZmC5Il9k4lsbzkccYiWpZkfj/aIWUWY8SeZlsuTvIkUvqc5GVKZIUWJioJ5ZTo1MnyY+OiX8YlZLF8201JZRpaZcSGJuwGYoGaZqpuY0eaZPGd49UuZiWCTqyiY3zeDJkOY6+GX6ESJsxmHhPOZksWIg/+Ztd2YioWZWluZGYCZqRqZpSyYxoSYjhGRyP+ITcmIzlmJ0HeZ1uOUpiCZOEuZYLKZ3PWZb0OZf2GZ3AyJlT1JdwuZ3eGZMAKo+lqZsd2Zz+aY3c6YQQepM06Zza85rTJZ55M5f62aGNGYsGuojDCJXa2JsXCqITyZIAmWWLQ5oCepzF2Z6nmJnZspccepZUdJr2uJMfqoTkeaDyqZzURsiRs+mSwvmj+Bmk9IhTOIp/J+qjNwqkOuqZMVmk4HKlWJqlWrqlXNqlXvqlYBqmYjqmZFqmZnqmaJqmarqmbNqmbqqmAQEAOw=='
def dodge(a, b, alpha):
return min(int(a * 255 / (256 - b * alpha)), 255)
def draw(dir_info, blur=25, alpha=1.0):
base_dir = os.path.dirname(os.path.abspath(__file__))
save_file_name = os.path.join(base_dir, 'final.png')
img = Image.open(dir_info)
img1 = img.convert('L') # 图片转换灰色
img2 = img1.copy()
img2 = ImageOps.invert(img2)
for i in range(blur):
img2 = img2.filter(ImageFilter.BLUR)
width, height = img1.size
for x in range(width):
for y in range(height):
a = img1.getpixel((x, y))
b = img2.getpixel((x, y))
img1.putpixel((x, y), dodge(a, b, alpha))
img1.save(save_file_name)
img1.show()
class LoginPage(object):
def __init__(self, master=None):
self.root = master
self.root.geometry('%dx%d' % (420, 240))
self.page = Frame(self.root)
self.Dir = StringVar()
self.Port = StringVar()
self.path = StringVar()
self.dir_info = StringVar()
self.create_page()
def create_page(self):
self.page.grid()
with open('tmp.gif', 'wb+') as f:
f.write(base64.b64decode(img_bs64))
self.photo = PhotoImage(file='tmp.gif')
os.remove('tmp.gif')
Label(self.page, text='''
作者 : 王翔
时间 :2019-08-06
工具 :Python 3.7.3 Tkinter
详情 : 将图片转化为素描画''', justify=LEFT).grid(row=0, column=0, columnspan=2, rowspan=1, stick=NW)
Label(self.page, text="图片路径").grid(row=3, column=0, sticky=W, pady=5)
self.dir_info = Entry(self.page, textvariable=self.path)
self.dir_info.grid(row=3, column=1, columnspan=1, padx=20)
Button(self.root, text="选择路径", command=lambda: self.select_path()).grid(row=0, column=0, sticky=S, padx=20,
pady=5)
Label(self.page, image=self.photo).grid(row=0, column=2)
Button(self.page, text='转换', command=self.login_check, width=10).grid(row=3, column=2, padx=10, pady=5)
def select_path(self):
path_ = askopenfilename(filetypes=[("file", "*.*")])
self.path.set(path_)
def login_check(self):
img_dir = self.dir_info.get()
if img_dir == "":
showinfo(title='错误', massage='路径错误')
else:
draw(img_dir)
root = Tk()
root.title('素描画转化工具')
LoginPage(root)
root.mainloop()
The End
OK,今天的内容就到这里