This Python script creates a simple desktop launcher using Tkinter with ttkbootstrap for modern styling. The application displays a window with a button that, when clicked, opens a Flask-based web application in a native window using pywebview. This approach combines a lightweight desktop interface with a web-powered backend, making it easy to deploy existing web apps as desktop applications.
Preliminary
Before I begin, please activate the virtual environment and install the required dependencies.
python -m venv venv
venv\Scripts\activate
pip install ttkbootstrap pywebview pyinstaller
Then all I need to do is create a Python file below.
What is PyWebView?
In fact, PyWebView is a lightweight Python library that lets me display web content (HTML, CSS, JavaScript, or even a full Flask app) inside a native desktop window. Since I have deployed Flask on PythonAnywhere in the previous tutorial. Therefore, all I need for this project is to embed the entire browser into the window.
Next, when the function is called, it will pop up a new webview window as shown below:Step 1: Set up a desktop launcher window
Before I set up the webview window, I created a launcher window as follows.
I chose "ttkBootstrap" as my GUI. My theme is "darkly," and the window dimensions are 400x200.
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
# Create TTKbootstrap window
root = ttk.Window(themename="darkly")
root.geometry("400x200")
root.title("Launcher")
# Create a button with open_app function
btn = ttk.Button(root, text="Open Web App", bootstyle=SUCCESS, command=open_app)
btn.pack(pady=50)
Here, I only create a button, "Open Web App," and when I click the button, it will pop up the webview window in step 2 below.Step 2: Embed the webview within the desktop window
Now I will embed the browser window with an HTML link "https://howapp.pythonanywhere.com/" inside the webview window, and here is my code:
import webview
def open_app():
# Create a new window titled "How App"
# and load the web application URL inside it
webview.create_window('How App', 'https://howapp.pythonanywhere.com/')
# Start the pywebview event loop (opens and runs the window)
webview.start()
Step 3: Package the ttkbootstrap desktop app
Before it can be distributed to the user to use, I need to package the desktop app. Therefore, in PowerShell, I will input the following code:
pyinstaller --onefile --noconsole `
--hidden-import=webview.platforms.edgechromium `
--hidden-import=webview.platforms.winforms `
webview_tkinter.pyWhy do I need to hide the above files? This is because in PyInstaller, a hidden import is a module that my app uses, but PyInstaller cannot detect automatically. Therefore, I will tell PyInstaller, “Hey, include these modules even if you didn’t detect them.”
So, overall, the above code means execute the PyInstaller to pack everything into one single executable file and hide the terminal and relevant imports, as I have mentioned above.
Final wrap-up:
In summary, PyWebview provides a simple way to turn your web-based application into a desktop experience by displaying it inside a native window. When combined with ttkbootstrap, you can create a clean Tkinter interface that acts as a launcher while your main application runs as a web app. This setup is especially useful if you already have a Flask project, allowing you to reuse your existing code while delivering it in a desktop-friendly format.
To view the full code, please click here



Comments
Post a Comment