教你用一行Python代碼實現(xiàn)GUI圖形界面
一、選擇文件夾
首先導(dǎo)入PySimpleGUI庫,并且用縮寫sg來表示。
import PySimpleGUI as sg
# 窗口顯示文本框和瀏覽按鈕, 以便選擇一個文件夾
dir_path = sg.popup_get_folder("Select Folder")
if not dir_path:
sg.popup("Cancel", "No folder selected")
raise SystemExit("Cancelling: no folder selected")
else:
sg.popup("The folder you chose was", dir_path)通過使用PySimpleGUI的popup_get_folder()方法,一行代碼就能實現(xiàn)選擇文件夾的操作。
示例如下

點擊Browse按鈕,選擇文件夾,文本框就會顯示出文件夾的絕對路徑。
點擊OK按鈕,顯示最終選擇的路徑信息,再次點擊OK按鈕,結(jié)束窗口。

如果沒有選擇文件夾,而是直接點擊OK按鈕,會直接提示沒有選取文件夾。
二、選擇文件
選擇文件操作和上面選擇文件夾的有點相似。
# 窗口顯示文本框和瀏覽按鈕, 以便選擇文件
fname = sg.popup_get_file("Choose Excel file", multiple_files=True, file_types=(("Excel Files", "*.xls*"),),)
if not fname:
sg.popup("Cancel", "No filename supplied")
raise SystemExit("Cancelling: no filename supplied")
else:
sg.popup("The filename you chose was", fname)不同的是,選擇文件可以設(shè)置multiple_files(是否為多個文件)和file_types(文件類型)參數(shù)。
示例如下

選擇了多個Excel文件,最終結(jié)果返回了所有文件的路徑地址。
三、選擇日期
使用popup_get_date()方法,顯示一個日歷窗口。
# 窗口顯示文本框和瀏覽按鈕, 以便選擇文件
fname = sg.popup_get_file("Choose Excel file", multiple_files=True, file_types=(("Excel Files", "*.xls*"),),)
if not fname:
sg.popup("Cancel", "No filename supplied")
raise SystemExit("Cancelling: no filename supplied")
else:
sg.popup("The filename you chose was", fname)示例如下

選擇好日期后,點擊OK按鈕,即可返回日期元組結(jié)果。
四、輸入文本
使用popup_get_text()方法,顯示一個文本輸入框。
# 顯示文本輸入框, 輸入文本信息, 返回輸入的文本, 如果取消則返回None
text = sg.popup_get_text("Please enter a text:")
if not text:
sg.popup("Cancel", "No text was entered")
raise SystemExit("Cancelling: no text entered")
else:
sg.popup("You have entered", text)鍵入信息,示例如下

點擊OK按鈕,返回輸入的文本信息。
如果沒有輸入,直接點擊OK按鈕,會提示沒有文本輸入。
五、彈窗無按鈕
# 顯示一個彈窗, 但沒有任何按鈕
sg.popup_no_buttons("You cannot click any buttons")結(jié)果如下

六、彈窗無標(biāo)題
# 顯示一個沒有標(biāo)題欄的彈窗
sg.popup_no_titlebar("A very simple popup")結(jié)果如下

七、彈窗只有OK按鈕
# 顯示彈窗且只有OK按鈕
sg.popup_ok("You can only click on 'OK'")結(jié)果如下

八、彈窗只有Error按鈕(紅色)
# 顯示彈窗且只有error按鈕, 按鈕帶顏色
sg.popup_error("Something went wrong")結(jié)果如下

九、顯示通知窗口
# 顯示一個“通知窗口”, 通常在屏幕的右下角, 窗口會慢慢淡入淡出
sg.popup_notify("Task done!")結(jié)果如下, Task done提示信息淡入淡出。

十、彈窗選擇
# 顯示彈窗以及是和否按鈕, 選擇判斷
answer = sg.popup_yes_no("Do you like this video?")
sg.popup("You have selected", answer)結(jié)果如下

十一、自定義彈窗
上面那些彈窗都是庫自帶的,如果想自定義創(chuàng)建,可以參考下面的方法。
# 自定義創(chuàng)建彈窗, 一行代碼完成
choice, _ = sg.Window(
"Continue?",
[[sg.T("Do you want to subscribe to this channel?")], [sg.Yes(s=10), sg.No(s=10), sg.Button('Maybe', s=10)]],
disable_close=True,
).read(close=True)
sg.popup("Your choice was", choice)結(jié)果如下

實戰(zhàn)
最后來個綜合實戰(zhàn)案例,將某個文件夾下所有的Excel文件中的sheet表,一一保存為單獨的Excel文件。
代碼如下,需要安裝xlwings庫,其中pathlib庫是內(nèi)置的。
from pathlib import Path
import PySimpleGUI as sg
import xlwings as xw
# 選擇輸入文件夾
INPUT_DIR = sg.popup_get_folder("Select an input folder")
if not INPUT_DIR:
sg.popup("Cancel", "No folder selected")
raise SystemExit("Cancelling: no folder selected")
else:
INPUT_DIR = Path(INPUT_DIR)
# 選擇輸出文件夾
OUTPUT_DIR = sg.popup_get_folder("Select an output folder")
if not OUTPUT_DIR:
sg.popup("Cancel", "No folder selected")
raise SystemExit("Cancelling: no folder selected")
else:
OUTPUT_DIR = Path(OUTPUT_DIR)
# 獲取輸入文件夾中所有xls格式文件的路徑列表
files = list(INPUT_DIR.rglob("*.xls*"))
with xw.App(visible=False) as app:
for index, file in enumerate(files):
# 顯示進度
sg.one_line_progress_meter("Current Progress", index + 1, len(files))
wb = app.books.open(file)
# 提取sheet表為單獨的Excel表格
for sheet in wb.sheets:
wb_new = app.books.add()
sheet.copy(after=wb_new.sheets[0])
wb_new.sheets[0].delete()
wb_new.save(OUTPUT_DIR / f"{file.stem}_{sheet.name}.xlsx")
wb_new.close()
sg.popup_ok("Task done!")
首先選擇輸入文件夾和輸出文件夾的地址。
然后通過pathlib庫對輸入文件夾進行遍歷,查找出所有xls格式文件的路徑地址。

點擊OK按鈕后,就會開始表格轉(zhuǎn)換,操作如下。

使用了one_line_progress_meter()方法顯示程序處理的進度。

20表示有20次循環(huán),原始Excel文件總計有20個,需要處理20次,其他的都在上圖中標(biāo)示出來咯。
到此這篇關(guān)于教你用一行Python代碼實現(xiàn)GUI圖形界面的文章就介紹到這了,更多相關(guān)Python GUI圖形界面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
TensorFlow和keras中GPU使用的設(shè)置操作
這篇文章主要介紹了TensorFlow和keras中GPU使用的設(shè)置操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
conda?install?nb_conda失敗原因分析及解決
這篇文章主要給大家介紹了關(guān)于conda?install?nb_conda失敗原因分析及解決方法,conda install nb_conda顯示錯誤的原因可能有很多,具體原因取決于你的系統(tǒng)環(huán)境和安裝的conda版本,需要的朋友可以參考下2023-11-11
python中如何用time方法生成當(dāng)前時間年月日時分秒
這篇文章主要給大家介紹了關(guān)于python中如何用time方法生成當(dāng)前時間年月日時分秒的相關(guān)資料,在Python中與時間處理有關(guān)的模塊就包括:time,datetime以及calendar,Time模塊用以取得系統(tǒng)時間相關(guān)的信息和時間的格式化等操作,需要的朋友可以參考下2023-08-08

