如何使用Python(Tkinter)建立直接可執行的跨平臺GUI應用程式?


Python是一種程式語言,可用於建立跨平臺應用程式,這些應用程式在各種作業系統(如Microsoft Windows、Mac OS和Linux)中受支援。

要建立基於GUI的應用程式,我們可以使用Tkinter庫。但是,Python提供了不同的模組和擴充套件,可以將程式轉換為可執行應用程式。

  • 對於Windows可執行檔案 - PyInstaller、py2exe

  • 對於Linux可執行檔案 - Freeze

  • 對於Max可執行檔案 - py2app

示例

對於此示例,我們首先將使用pip在Windows作業系統中安裝PyInstaller模組。可以使用以下命令安裝該模組:

pip install pyInstaller

使用此模組,我們將應用程式轉換為可執行檔案。

app.py

在此應用程式中,我們將要求使用者從列表中選擇特定日期。

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame
win = Tk()

# Set the size of the tkinter window
win.geometry("700x350")

# Add a Label
Label(win, text="Select a Day from the Menu",
   font=('Aerial 13')).pack(pady=10)

# Create a Variable to store the selection
var = StringVar()

# Create an OptionMenu Widget and add choices to it
option = OptionMenu(win, var, "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
option.config(bg="gray81", fg="white")
option['menu'].config(bg="green3")
option.pack(padx=20, pady=30)

win.mainloop()

輸出

在Python中建立可執行檔案的步驟

  • 在目錄中開啟命令提示符,並編寫建立可執行檔案的命令:

pyinstaller app.py
  • 它將建立一個包含可執行檔案app.exe的資料夾。開啟檔案以執行應用程式。

更新於: 2021年6月18日

1K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告