wxpython之Hyperlinks 不指定

root , 2008/05/22 21:27 , Python , 评论(0) , 阅读(6038) , Via 本站原创 | |
Hyperlinks
AppSnap displays the website of an application amongst other details. It would be ideal to open the link in the default browser when clicked. Luckily for us, wxPython has a widget just for this.

Let’s create a hyperlink:

# Create hyperlink on an existing panel widget
link = wx.lib.hyperlink.HyperLinkCtrl(parent=panel, pos=(225, 60))

For starters, let’s initialize the link.

# Initialize the hyperlink
link.SetURL(URL=”)
link.SetLabel(label=”)
link.SetToolTipString(tip=”)

The SetURL() method sets the URL that is invoked when the widget is clicked. The actual text displayed can be set using SetLabel(). The SetToolTipString() method controls the string displayed when the mouse is hovered over the widget. In AppSnap, I use all three of these methods to control the display. First, I use SetURL() to set the application URL. I then use SetLabel() to display a trimmed down version of the URL if it is too long. This is because long links do not fit on the panel. If the label has been trimmed, I use SetToolTipString() to display the entire URL when the mouse is hovered over the widget. This is so that the user can see the entire URL that will be opened on click.

# Setup the hyperlink for a short URL
link.SetURL(URL=’http://www.google.com’)
link.SetLabel(label=’http://www.google.com’)
link.SetToolTipString(tip=”)

# Setup the hyperlink for a long URL
link.SetURL(URL=’http://blog.genotrance.com/2006/08/19/wxpython-widgets-part-i/’)
link.SetLabel(label=’http://blog.genotrance.com/…’)
link.SetToolTipString(tip=’http://blog.genotrance.com/2006/08/19/wxpython-widgets-part-i/’)

On clicking the widget, the link is automatically opened in the default browser and the link color changes from blue to purple. Considering I use the same hyperlink widget for all the applications, once a user clicks on the URL for one application, the URLs for all applications show up as visited. Since I didn’t care about marking links as visited, I used the following method to ensure that links always show up in blue.

# Set the visited color as blue
link.SetColours(visited=wx.Colour(0, 0, 255))

Alternatively, one could use the SetVisited() method every time a new application link is to be displayed.

# Set the hyperlink as not visited
link.SetVisited(Visited=False)
Tags:
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]