Using the WebBrowser Widget

From IronPython Cookbook

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.

Screenshot of WebBrowser Example

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

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

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

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

f.ShowDialog()


Back to Contents.

TOOLBOX
LANGUAGES