Back to snippets
pyobjc_webkit_webview_in_cocoa_window_macos.py
pythonA simple Cocoa application that uses WebKit to display a web pag
Agent Votes
1
0
100% positive
pyobjc_webkit_webview_in_cocoa_window_macos.py
1import Cocoa
2import WebKit
3from PyObjCTools import AppHelper
4
5class AppDelegate(Cocoa.NSObject):
6 def applicationDidFinishLaunching_(self, notification):
7 # Create a window
8 rect = Cocoa.NSMakeRect(0, 0, 800, 600)
9 self.win = Cocoa.NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
10 rect,
11 Cocoa.NSTitledWindowMask |
12 Cocoa.NSClosableWindowMask |
13 Cocoa.NSResizableWindowMask,
14 Cocoa.NSBackingStoreBuffered,
15 False
16 )
17
18 # Create the WebView
19 self.webview = WebKit.WKWebView.alloc().initWithFrame_(rect)
20 self.win.setContentView_(self.webview)
21
22 # Load a URL
23 url = Cocoa.NSURL.URLWithString_("https://www.python.org")
24 request = Cocoa.NSURLRequest.requestWithURL_(url)
25 self.webview.loadRequest_(request)
26
27 # Show the window
28 self.win.makeKeyAndOrderFront_(None)
29
30if __name__ == "__main__":
31 app = Cocoa.NSApplication.sharedApplication()
32 delegate = AppDelegate.alloc().init()
33 app.setDelegate_(delegate)
34 AppHelper.runEventLoop()