如何利用python写GUI及生成.exe可执行文件
作者:画外人易朽 发布时间:2023-06-26 00:42:57
标签:python,gui,.exe
一.GUI(Graphical User Interface(图形用户接口))
1.导入需要用到的包
import tkinter as tk
import tkinter.messagebox
import copy
import os
2.获取文件夹中所有图片
def get_picture(dirs):
'''获得所有图片'''
picture_list = []
for dir, dir_abs, files in os.walk(dirs):
for file in files:
if file.endswith('.png'): # 注意检查分析数据格式
picture_list.append(os.path.join(dir, file))
return picture_list
3.定义一个类windows
class Window:
button_list = []
object_list = []
pictures = get_picture("F:\\pic\\class-1\\classresnet\\data\\dangerous")
file = pictures[0]
is_show = True
index = 0
image_file = ''
4.创建窗口和frame
def __init__(self):
'''创建窗口和frame'''
self.window = tk.Tk()
self.window.title('护目镜安全检测')
self.window.geometry('600x400')
self.frame = tk.Frame(self.window)
self.frame.pack()
self.frame_l = tk.Frame(self.frame)
self.frame_r = tk.Frame(self.frame)
self.frame_l.pack(side='left')
self.frame_r.pack(side='right')
self.frame_ll = tk.Frame(self.frame_r)
self.frame_rr = tk.Frame(self.frame_r)
self.frame_ll.pack(side='left')
self.frame_rr.pack(side='right')
5.定义需要用到的函数(下一页、上一页等按钮要用到的)
def next_picture(self):
'''下一张图片'''
self.index = self.pictures.index(self.file)
self.index += 1
if self.index < len(self.pictures):
self.checkout_button()
self.file = self.pictures[self.index]
self.create_canvas(self.file)
else:
self.index = len(self.pictures) - 1
tkinter.messagebox.showinfo('提示', '已近是最后一张了')
def checkout_button(self):
'''判断列表中是否只有button对象'''
object_list_copy = copy.copy(self.object_list)
for ob in self.object_list:
if ob in self.button_list:
pass
else:
b = object_list_copy.pop(self.object_list.index(ob))
b.destroy()
self.object_list = object_list_copy
def pre_picture(self):
'''上一页'''
self.index = self.pictures.index(self.file)
self.index -= 1
if self.index >= 0:
self.checkout_button()
self.file = self.pictures[self.index]
self.create_canvas(self.file)
else:
self.index = 0
tkinter.messagebox.showinfo('提示', '已经是第一张了')
def show_picture(self):
'''展示图片和翻页按钮'''
self.file = self.pictures[0]
if self.is_show:
self.is_show = False
self.create_canvas(self.file)
button1 = tk.Button(self.frame_ll, text='上一张', font=('Arial', 12), width=10, height=1, bg='orange',
command=self.pre_picture, relief='ridge', )
button1.pack()
button2 = tk.Button(self.frame_rr, text='下一张', font=('Arial', 12), width=10, height=1, bg='orange',
command=self.next_picture, relief='ridge', )
button2.pack()
self.button_list.append(button1)
self.button_list.append(button2)
self.object_list.extend(self.button_list)
else:
self.is_show = True
while self.object_list:
o = self.object_list.pop()
o.destroy()
6.创建按钮、画布,调用主程序
def new_button(self):
'''创建展示按钮'''"开始检测和显示结果可在此处新添加tk.button"
tk.Button(self.frame_l, text='开始读取', font=('Arial Black', 12), width=10, height=1, bg='green',
command=self.show_picture, relief='ridge').pack()
# tk.Button(self.frame_l, text='开始检测', font=('Arial Black', 12), width=10, height=1, bg='blue',command=classresnet, relief='ridge').pack()
def create_canvas(self, file):
'''用画布展示图片'''
self.image_file = tk.PhotoImage(file=file)
canvas = tk.Canvas(self.frame_r, height=500, width=600, bg='gray')
canvas.create_image(1, 1, anchor='nw', image=self.image_file)
canvas.pack()
self.object_list.append(canvas)
def run(self):
'''主程序调用'''
self.window.mainloop()
if __name__ == '__main__':
w = Window()
w.new_button()
w.run()
效果展示
完整代码
import tkinter as tk
import tkinter.messagebox
import copy
import os
# from glob2 import glob
# import main
"""Graphical User Interface(图形用户接口)"""
def get_picture(dirs):
'''获得所有图片'''
picture_list = []
for dir, dir_abs, files in os.walk(dirs):
for file in files:
if file.endswith('.png'): # 注意检查分析数据格式
picture_list.append(os.path.join(dir, file))
return picture_list
class Window:
button_list = []
object_list = []
# for pngfile in glob("F:\\pic\\class-1\\classresnet\\datas\\*.png"):
# main.image_demo()
# output_dir = "F:\\pic\\class-1\\classresnet\\data\\try" # 保存截取的图像目录
# print('图片获取完成 。。。!')
#
# main.classresnet()
pictures = get_picture("F:\\pic\\class-1\\classresnet\\data\\dangerous")
file = pictures[0]
is_show = True
index = 0
image_file = ''
def __init__(self):
'''创建窗口和frame'''
self.window = tk.Tk()
self.window.title('护目镜安全检测')
self.window.geometry('600x400')
self.frame = tk.Frame(self.window)
self.frame.pack()
self.frame_l = tk.Frame(self.frame)
self.frame_r = tk.Frame(self.frame)
self.frame_l.pack(side='left')
self.frame_r.pack(side='right')
self.frame_ll = tk.Frame(self.frame_r)
self.frame_rr = tk.Frame(self.frame_r)
self.frame_ll.pack(side='left')
self.frame_rr.pack(side='right')
def next_picture(self):
'''下一张图片'''
self.index = self.pictures.index(self.file)
self.index += 1
if self.index < len(self.pictures):
self.checkout_button()
self.file = self.pictures[self.index]
self.create_canvas(self.file)
else:
self.index = len(self.pictures) - 1
tkinter.messagebox.showinfo('提示', '已近是最后一张了')
def checkout_button(self):
'''判断列表中是否只有button对象'''
object_list_copy = copy.copy(self.object_list)
for ob in self.object_list:
if ob in self.button_list:
pass
else:
b = object_list_copy.pop(self.object_list.index(ob))
b.destroy()
self.object_list = object_list_copy
def pre_picture(self):
'''上一页'''
self.index = self.pictures.index(self.file)
self.index -= 1
if self.index >= 0:
self.checkout_button()
self.file = self.pictures[self.index]
self.create_canvas(self.file)
else:
self.index = 0
tkinter.messagebox.showinfo('提示', '已经是第一张了')
def show_picture(self):
'''展示图片和翻页按钮'''
self.file = self.pictures[0]
if self.is_show:
self.is_show = False
self.create_canvas(self.file)
button1 = tk.Button(self.frame_ll, text='上一张', font=('Arial', 12), width=10, height=1, bg='orange',
command=self.pre_picture, relief='ridge', )
button1.pack()
button2 = tk.Button(self.frame_rr, text='下一张', font=('Arial', 12), width=10, height=1, bg='orange',
command=self.next_picture, relief='ridge', )
button2.pack()
self.button_list.append(button1)
self.button_list.append(button2)
self.object_list.extend(self.button_list)
else:
self.is_show = True
while self.object_list:
o = self.object_list.pop()
o.destroy()
# def code_button(self):
# tk.Button(self.frame_l, text='开始检测', font=('Arial Black', 12), width=10, height=1, bg='blue',
# command=main.classresnet, relief='ridge').pack()
def new_button(self):
'''创建展示按钮'''"开始检测和显示结果可在此处新添加tk.button"
tk.Button(self.frame_l, text='开始读取', font=('Arial Black', 12), width=10, height=1, bg='green',
command=self.show_picture, relief='ridge').pack()
# tk.Button(self.frame_l, text='开始检测', font=('Arial Black', 12), width=10, height=1, bg='blue',command=classresnet, relief='ridge').pack()
def create_canvas(self, file):
'''用画布展示图片'''
self.image_file = tk.PhotoImage(file=file)
canvas = tk.Canvas(self.frame_r, height=500, width=600, bg='gray')
canvas.create_image(1, 1, anchor='nw', image=self.image_file)
canvas.pack()
self.object_list.append(canvas)
def run(self):
'''主程序调用'''
self.window.mainloop()
if __name__ == '__main__':
w = Window()
w.new_button()
w.run()
二.生成exe文件
在windows下,可以使用pyinstaller打包python程序为exe可执行程序。
1.安装pyinstaller
在cmd命令行窗口运行以下命令安装pyinstaller
pip install pyinstaller
2.打包python程序
在python程序所在目录,执行以下命令
pyinstaller -F xxx.py -w
注:如果不加-w,生成的exe文件会同时出现命令行窗口
3.运行exe文件
打包完成后,在对应目录会出现build和dist文件夹,exe文件就出现在dist文件夹,直接运行即可。
4.常用命令参数
(1) -F 指定打包后只生成一个exe格式的文件(dist文件只有一个exe格式的文件T1)
pyinstaller -F T1.py
(2) -i 改变生成程序的icon图标
pyinstaller -F -i ./my.ico T1.py
(3) -n NAME,–name=NAME 设置产生文件的名字(mypy)
pyinstaller -F -n mypy -i ./my.ico T1.py
效果展示
执行exe应用
因为是exe应用,是可执行文件了,所以直接双击运行即可,运行效果如下图所示:
来源:https://blog.csdn.net/WangNning2000/article/details/117046031


猜你喜欢
- function clickButton(id) { &n
- 本文实例讲述了Python使用matplotlib绘制三维图形。分享给大家供大家参考,具体如下:用二维泡泡图表示三维数据泡泡的坐标2维,泡泡
- 起因是这样的,有一张表存在慢sql,查询耗时最多达到12s,定位问题后发现是由于全表扫描导致,需要对字段增加索引,但是表的数据量600多万有
- 对数据库的管理常规就是进行预防性的维护,以及修复那些出现问题的内容。进行检查和修复通常具有四个主要的任务:1. 对表进行优化2. 对表进行分
- <html xmlns="http://www.w3.org/1999/xhtml"><head>
- 如: 0.625 取 1 2.1 取3 3.6 取4 <% if fix(a)>a then b=fix(a) else b=f
- 一、散点图散点图用两组数据构成多个坐标点,考察坐标点的分布,判断两变量之间是否存在某种关联或总结坐标点的分布模式。特点:判断变量之间是否存在
- 在PyQt中没有直接提供左键双击的判断方法,需要自己实现,其思路主要如下所示:1、起动一个定时器,判断在指定的时间之内,点击次数
- #!/usr/bin/env pythonimport sys,timefrom socket import socketdef read_
- 前言本文主要使用 cpu 版本的 tensorflow 2.4 版本完成文本的 word embedding 训练,并且以此为基础完成影评文
- Microsoft? SQL Server? 2000 提供了两种主要机制来强制业务规则和数据完整性:约束和触发器。触发器是一种特殊类型的存
- 内容摘要:为什么要什么XML文件:其优势就是处理该XML数据的文档可以是静态文档,比如HTML文件通过Javascript、XMLDOM来解
- 1. 关于 try.. finally..假如上帝用 python 为每一个来到世界的生物编写程序,那么除去中间过程的种种复杂实现,最不可避
- 本文实例为大家分享了pygame实现弹球游戏的具体代码,供大家参考,具体内容如下pygame弹球游戏写的很简陋pip install pyg
- IE的有条件注释是一种专有的(因此是非标准的)、对常规(X)HTML注释的Miscrosoft扩展。顾名思义,有条件注释使你能够根据条件(比
- 数据结构&Series:'''series {索引 + 数据} 形式索引是自动生成的''
- Pycharm应该是学python必用的编辑器了,关于它的使用之前已经写过几篇文章,今天再给大家继续介绍两个pycharm的小技巧,希望对大
- 阅读上一篇教程:WEB2.0网页制作标准教程(8)CSS布局入门接下来开始要真正设计布局了。和传统的方法一样,你首先要在脑海里有大致的轮廓构
- 时间差函数TIMESTAMPDIFF、DATEDIFF的用法我们在写sql语句,尤其是存储过程中,会频繁用到对于日期、时间的比较和判断,那么
- 1.sort()方法sort()是列表的方法,修改原列表使得它按照大小排序,没有返回值,返回NoneIn [90]: x = [4, 6,