Using the WebBrowser Widget

Using the WebBrowser Widget
This example shows how to use WebBrowser. Specifically, it shows the usage of the Navigate method and how to handle the Navigated event.

The code includes a conditional near the top that allows it to work with Python.NET.



url = 'http://google.com'

import sys if sys.platform != 'cli': import pythoncom pythoncom.CoInitialize

import clr clr.AddReference('System.Windows.Forms') from System.Windows.Forms import Form, DockStyle, WebBrowser, TextBox, Keys

f = Form f.Text = 'WebBrowser Example' f.Width = 800 f.Height = 600

wb = WebBrowser wb.Navigate(url)

tb = TextBox tb.Text = url

tb.Dock = DockStyle.Top wb.Dock = DockStyle.Fill f.Controls.Add(wb) f.Controls.Add(tb)
 * 1) Layout

def OnKeyPress(sender, args): if args.KeyChar == u'\r': wb.Navigate(tb.Text.strip) tb.KeyPress += OnKeyPress
 * 1) Event handling

def OnNavigated(sender, args): tb.Text = args.Url.ToString wb.Navigated += OnNavigated

f.ShowDialog

Back to Contents.