Pyjs / Pyjamas框架:如何使用按钮更改框架中的URL

时间:2012-06-21 02:53:14

标签: button frame pyjamas

我有一个包含两个按钮和一个框架的网页。在框架内,显示网页。我试图让它成为框架中的按钮B鞋url'/ AAA'按钮B鞋url'/ BBB'。我怎么能这样做?

这就是我所拥有的:

class ImageButton(SimplePanel):
    def __init__(self, image_location):
        '''(None, str) -> None'''

        SimplePanel.__init__(self)

        img = Image(image_location, StyleName='rangler')
        img.addClickListener(getattr(self, "onImageClick"))
        self.add(img)


    def onImageClick(self, sender=None):

        pass
        #This is where I need help!?

class QAFrame(SimplePanel):
    def __init__(self, current_url):
        SimplePanel.__init__(self)

        frame = Frame(current_url,
                      Width="200%",
                      Height="650px")
        self.add(frame)


def caption():
    style_sheet = HTML("""<link rel='stylesheet' href='about_us.css'>""")
    srah_pic = ImageButton("Steve.jpg")
    fl_pic = ImageButton("Fraser.jpg")  

    horizontal = HorizontalPanel()
    vertical = VerticalPanel()

    vertical.add(srah_pic)
    vertical.add(fl_pic)
    horizontal.add(vertical)

    QAFrame('Fraser_qa.htm')
    QAFrame('Steve_qa.htm')

    horizontal.add(QAFrame('Steve_qa.htm'))



    RootPanel().add(horizontal)

2 个答案:

答案 0 :(得分:1)

基本上,

您需要.addClickListener到您的按钮,并且,作为参数,您希望传递将在按钮单击时执行所需任务的处理程序。

让我困惑的一件事是,我无法将论证传递给我的经纪人。但是,对象“sender”会自动传入处理程序。您可以尝试捕获发件人属性以查找所需的信息。

class ImageButton(SimplePanel):

    def __init__(self, image_location, css_style):
        '''(None, str, str) -> None'''

        SimplePanel.__init__(self)

        self.image_location = image_location

        img = Image(self.image_location, StyleName= css_style)
        img.addClickListener(Cool)   # Cool is the name of my handler function
        self.add(img)

def Cool(sender):   #   You can do whatever you want in your handler function.
                    #   I am changing the url of a frame
                    #   It is a little-medium "Hacky"

    if sender.getUrl()[-9:] == 'Steve.jpg':
        iframe.setUrl('Fraser_qa.htm')
    else:
        iframe.setUrl('Steve_qa.htm')

答案 1 :(得分:0)

我会扩展我的ImageButton类,以支持将URL传递到您想要显示的网页。在__init__函数中,您可以将该URL存储在实例属性中。

您应该将clickhandler变为实例方法,该方法可以访问保存所需页面的URL的实例变量。

我缺乏明确的Python知识来提供代码示例。希望你仍然理解这个概念。