使用Python中tkinter庫簡單gui界面制作及打包成exe的操作方法(二)
上一篇我們寫了怎么將xmind轉(zhuǎn)換成想要的excel格式,這篇再講一下用Python自帶的tkinter庫設計一個簡單的gui界面,讓我們的xmind路徑,用例版本執(zhí)行等都通過這個gui界面來輸入,生成我們需要的excel文件。
Python要生成gui,庫還是比較多的比如wxpython,這個我看了下,感覺比較難懂,畢竟只是設計一個比較簡單的gui界面,所以就使用了tkinter庫,感覺這個還是比較方便易懂的,大家可以在這里學習tkinter庫http://c.biancheng.net/python/tkinter/
如果只是想做個簡單的gui界面,直接想用什么就找什么就行了 ,我這個界面只用到了askopenfilename 和showinfo這兩個功能,askopenfilename 是用來選擇獲取Windows文件路徑的,showinfo是用來彈出提示框的,另外一個注意點就是 “self.module = tk.StringVar() self.secEntry = tk.Entry(self,textvariable = self.module)”,必須要這樣寫才能獲取到輸入框的值,剛開始我不知道這樣獲取,也是找了半天才找到,from xmindtoxls import xmind_to_xls是上一篇寫的生成excel的文件,可以把兩個文件放到一個文件夾下
import tkinter as tk
from tkinter.filedialog import askopenfilename
from xmindtoxls import xmind_to_xls
from tkinter.messagebox import showinfo
import re
# 定義MainUI類表示應用/窗口,繼承Frame類
class MainUI(tk.Frame):
# Application構造函數(shù),master為窗口的父控件
def __init__(self, master=None):
# 初始化Application的Frame部分
tk.Frame.__init__(self, master)
# 顯示窗口,并使用grid布局
self.grid()
self.path = tk.StringVar()
# 創(chuàng)建控件
self.createWidgets()
def selectPath(self):
'''選擇要轉(zhuǎn)換成excel的xmind地址'''
self.path_ = askopenfilename()
self.path.set(self.path_)
# 創(chuàng)建控件
def createWidgets(self):
'''生成gui界面'''
# 創(chuàng)建一個標簽,輸出要顯示的內(nèi)容
self.firstLabel = tk.Label(self, text="目標路徑")
# 設定使用grid布局
self.firstLabel.grid(row = 0, column = 0)
self.firstEntry = tk.Entry(self,textvariable = self.path)
self.firstEntry.grid(row=0, column=1)
# 創(chuàng)建一個按鈕,用來觸發(fā)answer方法
self.clickButton = tk.Button(self, text="路徑選擇", command=self.selectPath)
# 設定使用grid布局
self.clickButton.grid(row = 0, column = 2)
# 創(chuàng)建一個標簽,輸入模塊
self.secLabel = tk.Label(self, text="模塊")
# 設定使用grid布局
self.secLabel.grid(row=1, column=0)
self.module = tk.StringVar()
self.secEntry = tk.Entry(self,textvariable = self.module)
self.secEntry.grid(row=1, column=1)
# 創(chuàng)建一個標簽,輸入版本號
self.trLabel = tk.Label(self, text="版本號")
# 設定使用grid布局
self.trLabel.grid(row=2, column=0)
self.buildnum = tk.StringVar()
self.trEntry = tk.Entry(self,textvariable = self.buildnum)
self.trEntry.grid(row=2, column=1)
# 創(chuàng)建一個標簽,輸入執(zhí)行人
self.fourLabel = tk.Label(self, text="執(zhí)行人")
# 設定使用grid布局
self.fourLabel.grid(row=3, column=0)
self.owner = tk.StringVar()
self.fourEntry = tk.Entry(self,textvariable = self.owner)
self.fourEntry.grid(row=3, column=1)
# 創(chuàng)建一個提交按鈕,用來觸發(fā)提交方法,獲取值
self.clickButton = tk.Button(self, text="提交",command=self.getvalue)
# 設定使用grid布局
self.clickButton.grid(row=4, column=1)
def getvalue(self):
'''獲取輸入的值,并執(zhí)行轉(zhuǎn)換excel函數(shù)'''
global way,module,buildnum,owner
way = self.path.get()
module = self.module.get()
buildnum = self.buildnum.get()
owner = self.owner.get()
print(way,module,buildnum,owner)
self.regvalue = '.*\.xmind$'
self.xmind_reg = re.match(self.regvalue,way )
if self.xmind_reg:
# xmind轉(zhuǎn)換成xls
self.xmind_to_xls = xmind_to_xls()
self.xmind_to_xls.write_excel(way,module,buildnum,owner)
else:
showinfo(title='提示',message='請選擇正確的xmind文件,謝謝!')
# 創(chuàng)建一個MainUI對象
app = MainUI()
# 設置窗口標題
app.master.title('「xmind轉(zhuǎn)xls」')
# 設置窗體大小
app.master.geometry('400x200')
# 主循環(huán)開始
app.mainloop()
打包用的是pyinstaller庫,先pip安裝一下,然后進入相應的Python文件所在的文件夾下,輸入命令pyinstaller -Ftkintertoxls.py -pxmindtoxls.py 就可以生成exe可執(zhí)行文件了,這樣這個xmind轉(zhuǎn)xls的打包工具就完成了。注意電腦管家可能會把exe文件刪除掉,設置為安全就可以了。
到此這篇關于使用Python中tkinter庫簡單gui界面制作及打包成exe的操作方法(二)的文章就介紹到這了,更多相關Python gui界面制作及打包成exe內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Pytorch自己加載單通道圖片用作數(shù)據(jù)集訓練的實例
今天小編就為大家分享一篇Pytorch自己加載單通道圖片用作數(shù)據(jù)集訓練的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
利用Python制作動態(tài)排名圖的實現(xiàn)代碼
這篇文章主要介紹了利用Python制作動態(tài)排名圖的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
PyTorch?可視化工具TensorBoard和Visdom
這篇文章主要介紹了PyTorch?可視化工具TensorBoard和Visdom,TensorBoard?一般都是作為?TensorFlow?的可視化工具,與?TensorFlow?深度集成,它能夠展現(xiàn)?TensorFlow?的網(wǎng)絡計算圖,繪制圖像生成的定量指標圖以及附加數(shù)據(jù)等,下面來看文章得具體內(nèi)容介紹吧2022-01-01
使用Python進行大規(guī)模數(shù)據(jù)處理和分析
大規(guī)模數(shù)據(jù)處理和分析旨在從海量數(shù)據(jù)中提取有用的信息和見解,以支持決策制定和業(yè)務發(fā)展,Python憑借其豐富的生態(tài)系統(tǒng)和強大的庫,為處理和分析數(shù)據(jù)提供了豐富的工具和資源,在本文中,我們將深入探討如何利用Python進行大規(guī)模數(shù)據(jù)處理和分析,需要的朋友可以參考下2024-05-05
Pandas讀取MySQL數(shù)據(jù)到DataFrame的方法
今天小編就為大家分享一篇Pandas讀取MySQL數(shù)據(jù)到DataFrame的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

