【问题标题】:Pyjs / Pyjamas Frame: How to use a button to change the url in a framePyjs /睡衣框架:如何使用按钮更改框架中的url
【发布时间】:2012-07-27 16:37:58
【问题描述】:

我有一个包含两个按钮和一个框架的网页。在框架内,将显示一个网页。我正在尝试在框架中制作按钮 A 鞋子 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)

【问题讨论】:

    标签: button frame pyjamas


    【解决方案1】:

    基本上,

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

    真正让我困惑的一件事是,我无法将参数传递给我的处理程序。但是,对象“发送者”会自动与处理程序一起传入。您可以尝试通过 senders 属性来查找您需要的信息。

    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')
    

    【讨论】:

      【解决方案2】:

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

      您应该将 clickhandler 变成一个实例方法,该方法可以访问包含指向所需页面的 URL 的实例变量。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-23
        • 2011-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多