怎么利用python写GUI及生成.exe可执行文件 |
您所在的位置:网站首页 › python编写窗口 › 怎么利用python写GUI及生成.exe可执行文件 |
怎么利用python写GUI及生成.exe可执行文件,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。 一.GUI(Graphical User Interface(图形用户接口))1.导入需要用到的包import tkinter as tk import tkinter.messagebox import copy import os2.获取文件夹中所有图片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_list3.定义一个类windowsclass 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.创建窗口和framedef __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 = 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()效果展示在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应用,是可执行文件了,所以直接双击运行即可,运行效果如下图所示: 看完上述内容,你们掌握怎么利用python写GUI及生成.exe可执行文件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程笔记行业资讯频道,感谢各位的阅读! |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |