如何利用python寫GUI及生成.exe可執(zhí)行文件
一.GUI(Graphical User Interface(圖形用戶接口))
1.導(dǎo)入需要用到的包
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'): # 注意檢查分析數(shù)據(jù)格式
picture_list.append(os.path.join(dir, file))
return picture_list
3.定義一個(gè)類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.創(chuàng)建窗口和frame
def __init__(self):
'''創(chuàng)建窗口和frame'''
self.window = tk.Tk()
self.window.title('護(hù)目鏡安全檢測(cè)')
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.定義需要用到的函數(shù)(下一頁、上一頁等按鈕要用到的)
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對(duì)象'''
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('提示', '已經(jīng)是第一張了')
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.創(chuàng)建按鈕、畫布,調(diào)用主程序
def new_button(self):
'''創(chuàng)建展示按鈕'''"開始檢測(cè)和顯示結(jié)果可在此處新添加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='開始檢測(cè)', 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):
'''主程序調(diào)用'''
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'): # 注意檢查分析數(shù)據(jù)格式
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):
'''創(chuàng)建窗口和frame'''
self.window = tk.Tk()
self.window.title('護(hù)目鏡安全檢測(cè)')
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對(duì)象'''
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('提示', '已經(jīng)是第一張了')
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='開始檢測(cè)', font=('Arial Black', 12), width=10, height=1, bg='blue',
# command=main.classresnet, relief='ridge').pack()
def new_button(self):
'''創(chuàng)建展示按鈕'''"開始檢測(cè)和顯示結(jié)果可在此處新添加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='開始檢測(cè)', 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):
'''主程序調(diào)用'''
self.window.mainloop()
if __name__ == '__main__':
w = Window()
w.new_button()
w.run()
二.生成exe文件
在windows下,可以使用pyinstaller打包python程序?yàn)閑xe可執(zhí)行程序。
1.安裝pyinstaller
在cmd命令行窗口運(yùn)行以下命令安裝pyinstaller
pip install pyinstaller
2.打包python程序
在python程序所在目錄,執(zhí)行以下命令
pyinstaller -F xxx.py -w
注:如果不加-w,生成的exe文件會(huì)同時(shí)出現(xiàn)命令行窗口
3.運(yùn)行exe文件
打包完成后,在對(duì)應(yīng)目錄會(huì)出現(xiàn)build和dist文件夾,exe文件就出現(xiàn)在dist文件夾,直接運(yùn)行即可。
4.常用命令參數(shù)
(1) -F 指定打包后只生成一個(gè)exe格式的文件(dist文件只有一個(gè)exe格式的文件T1)
pyinstaller -F T1.py
(2) -i 改變生成程序的icon圖標(biāo)
pyinstaller -F -i ./my.ico T1.py
(3) -n NAME,–name=NAME 設(shè)置產(chǎn)生文件的名字(mypy)
pyinstaller -F -n mypy -i ./my.ico T1.py
效果展示

執(zhí)行exe應(yīng)用
因?yàn)槭莈xe應(yīng)用,是可執(zhí)行文件了,所以直接雙擊運(yùn)行即可,運(yùn)行效果如下圖所示:

總結(jié)
到此這篇關(guān)于如何利用python寫GUI及生成.exe可執(zhí)行文件的文章就介紹到這了,更多相關(guān)python寫GUI及生成.exe內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Windows下將Python文件打包成.EXE可執(zhí)行文件的方法
- 在python中實(shí)現(xiàn)調(diào)用可執(zhí)行文件.exe的3種方法
- 詳解將Python程序(.py)轉(zhuǎn)換為Windows可執(zhí)行文件(.exe)
- 將Python文件打包成.EXE可執(zhí)行文件的方法
- 將Python代碼打包成.exe可執(zhí)行文件的完整步驟
- python調(diào)用可執(zhí)行文件.exe的2種實(shí)現(xiàn)方法
- PyInstaller將Python腳本打包為.exe可執(zhí)行文件的步驟詳解
- 詳解如何將Python可執(zhí)行文件(.exe)反編譯為Python腳本
- Python打包成.exe可執(zhí)行文件的詳細(xì)步驟
- Python生成可執(zhí)行文件.exe操作完整流程記錄
相關(guān)文章
Python使用POP3和SMTP協(xié)議收發(fā)郵件的示例代碼
這篇文章主要介紹了Python使用POP3和SMTP協(xié)議收發(fā)郵件的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Python裝飾器實(shí)現(xiàn)方法及應(yīng)用場(chǎng)景詳解
這篇文章主要介紹了Python裝飾器實(shí)現(xiàn)方法及應(yīng)用場(chǎng)景詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
如何使用Python判斷應(yīng)用是否處于已打包狀態(tài)
在使用 PyInstaller 打包 Python 應(yīng)用時(shí),有時(shí)需要在代碼中判斷程序是否處于“打包狀態(tài)”,本文將介紹幾種方法來判斷是否處于打包狀態(tài),感興趣的可以了解下2025-03-03
Jupyter?notebook運(yùn)行后打不開網(wǎng)頁的問題解決
本文主要介紹了Jupyter?notebook運(yùn)行后打不開網(wǎng)頁的問題解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Python基于多線程實(shí)現(xiàn)ping掃描功能示例
這篇文章主要介紹了Python基于多線程實(shí)現(xiàn)ping掃描功能,結(jié)合實(shí)例形式分析了Python多線程與進(jìn)程相關(guān)模塊調(diào)用操作技巧,需要的朋友可以參考下2018-07-07
python區(qū)塊鏈簡(jiǎn)易版交易完善挖礦獎(jiǎng)勵(lì)示例
這篇文章主要介紹了python區(qū)塊鏈簡(jiǎn)易版交易完善挖礦獎(jiǎng)勵(lì)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
淺談對(duì)Python變量的一些認(rèn)識(shí)理解
變量(variable)是編程的基礎(chǔ)概念,Python 的變量看似簡(jiǎn)單,深入了解卻不易.文中有非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
python中的插值 scipy-interp的實(shí)現(xiàn)代碼
這篇文章主要介紹了python中的插值 scipy-interp的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-07-07
使用python搭建代理IP池實(shí)現(xiàn)接口設(shè)置與整體調(diào)度
在網(wǎng)絡(luò)爬蟲中,代理IP池是一個(gè)非常重要的組件,由于許多網(wǎng)站對(duì)單個(gè)IP的請(qǐng)求有限制,因此,我們需要一個(gè)代理IP池,在本文中,我們將使用Python來構(gòu)建一個(gè)代理IP池,然后,我們將使用這個(gè)代理IP池來訪問我們需要的數(shù)據(jù),文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2023-12-12

