Last active
September 20, 2025 05:11
-
-
Save ynkdir/ad30a799353e23da91cacc731ce1c5bd to your computer and use it in GitHub Desktop.
Example to use WebView2 with CoreWebView2Environment.CreateWithOptionsAsync()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Example to use WebView2 with CoreWebView2Environment.CreateWithOptionsAsync(). | |
| import asyncio | |
| from win32more.Microsoft.UI.Xaml import Window | |
| from win32more.Microsoft.UI.Xaml.Controls import WebView2 | |
| from win32more.Microsoft.Web.WebView2.Core import CoreWebView2Environment, CoreWebView2EnvironmentOptions | |
| from win32more.Windows.Foundation import Uri | |
| from win32more.appsdk.xaml import XamlApplication | |
| class App(XamlApplication): | |
| def OnLaunched(self, args): | |
| asyncio.get_running_loop().create_task(self._on_launched_async()) | |
| async def _on_launched_async(self): | |
| # Default browser folder will be "C:\Program Files (x86)\Microsoft\EdgeWebView\Application\140.0.3485.66" with specific version. | |
| # You can distribute your app with WebView2 runtime. | |
| # https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/distribution?tabs=dotnetcsharp#details-about-the-fixed-version-runtime-distribution-mode | |
| # When {browserExecutableFolder}\EBWebView\x64\EmbeddedBrowserWebView.dll is not found: | |
| # Error 0x80070002 "The system cannot find the file specified." | |
| # When browserExecutableFolder is not WebView2 runtime folder (e.g. C:\Program Files (x86)\Microsoft\Edge\...): | |
| # Error 0x80070032 "The request is not supported." | |
| browserExecutableFolder = None # Use None to get default browser folder | |
| # Default user data folder will be "path/to/python/python.exe.WebView2" | |
| userDataFolder = None # use None to get default user data folder | |
| options = CoreWebView2EnvironmentOptions() | |
| # set options... | |
| env = await CoreWebView2Environment.CreateWithOptionsAsync(browserExecutableFolder, userDataFolder, options) | |
| webview = WebView2() | |
| await webview.EnsureCoreWebView2WithEnvironmentAsync(env) | |
| webview.Source = Uri("https://www.microsoft.com/") | |
| win = Window() | |
| win.Content = webview | |
| win.Activate() | |
| def main(): | |
| XamlApplication.Start(App) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment