2012-06-21 25 views
0

我有一個包含兩個按鈕和一個框架的網頁。在框架內顯示網頁。我試圖讓它如此按鈕在鞋架中鞋子網址'/ AAA',而按鈕B在鞋架中'鞋子'/'BBB'。我怎麼能這樣做?Pyjs /睡衣框架:如何使用按鈕來更改框架中的url

以下是我有:

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) 

回答

1

基本上,

您需要.addClickListener你的按鈕,作爲一個參數,要在處理器來傳遞,將執行所需的任務點擊按鈕。

讓我很困惑的一件事是,我無法將一個參數傳遞給我的處理程序。但是,對象「發件人」會自動傳入處理程序。您可以嘗試瀏覽發件人屬性以查找所需的信息。

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

我會擴展我的ImageButton類以支持將URL傳遞到要顯示的網頁。在__init__函數中,可以將該URL存儲在實例屬性中。

您應該將clickhandler變成一個實例方法,該實例方法可以訪問包含指向所需頁面的URL的實例變量。

我缺乏明確的Python知識來提供代碼示例。希望你仍然理解這個概念。