2013-08-19 22 views
1

我在wxPython中創建了一個Huffington Post RSS聚合器,但我遇到了一些麻煩。在該程序中,主wx.Frame中有兩個面板:一個顯示所有文章的列表,另一個顯示用戶選擇的文章的Web視圖。我還沒有到那個部分,所以我決定通過加載Google來測試Web視圖窗口小部件。但是,當我這樣做時,我收到了一些奇怪的結果。下面是相關代碼:從wxPython獲取奇怪的結果WebView

hbox = wx.BoxSizer(wx.HORIZONTAL) 

listPanel = wx.Panel(self, -1, style=wx.SUNKEN_BORDER) 
htmlPanel = wx.Panel(self, -1, style=wx.SUNKEN_BORDER) 

browser = wx.html2.WebView.New(htmlPanel) 
browser.LoadURL("http://www.google.com") 

hbox.Add(listPanel, 1, wx.EXPAND) 
hbox.Add(htmlPanel, 2, wx.EXPAND) 

self.SetAutoLayout(True) 
self.SetSizer(hbox) 
self.Layout() 

,這裏是什麼,我得到一個畫面:

http://i.imgur.com/TVuKzRE.png

我似乎得到在左上角的文本框,可能是谷歌搜索框?不知道它是什麼或爲什麼我得到這個。如果有人碰巧看到我出錯的地方,我會非常感謝幫助。

編輯:

下面是一些可運行的代碼,顯示問題:

import wx 
import wx.html2 

class MainFrame(wx.Frame): 

    def __init__(self, *args, **kwargs): 
     super(MainFrame, self).__init__(*args, **kwargs) 
     self.InitUI() 
     self.Centre() 
     self.Show() 

    def InitUI(self): 
     hbox = wx.BoxSizer(wx.HORIZONTAL) 

     listPanel = wx.Panel(self, -1, style=wx.SUNKEN_BORDER) #This is the panel where the news articles would be shown 
     htmlPanel = wx.Panel(self, -1, style=wx.SUNKEN_BORDER) #This is the panel where the web view would be shown 

     browser = wx.html2.WebView.New(htmlPanel) #I create the new web view here with the htmlPanel as its parent 
     browser.LoadURL("http://www.google.com") #And then I load Google here 

     hbox.Add(listPanel, 1, wx.EXPAND) #Then I add both panels to the frame. Not sure where I went wrong. 
     hbox.Add(htmlPanel, 2, wx.EXPAND) 

     self.SetAutoLayout(True) 
     self.SetSizer(hbox) 
     self.Layout() 

def main(): 
    app = wx.App() 
    frame = MainFrame(None, title='What is this box? HELP!', size=(800,480)) 
    app.MainLoop() 


if __name__ == '__main__': 
    main() 
+0

你可以創建一個小的,可運行的例子嗎?這對我來說看起來很合適,但如果沒有一些可運行的代碼,我無法幫助。這是在Mac上運行嗎? –

+0

發佈代碼運行,忽略了所有的RSS術語。是的,這是在OS X Lion上運行的。 – JackSac67

回答

0

我似乎已經解決了這個問題。我沒有創建面板,然後嘗試將網絡視圖放到面板中,而是將網絡視圖的父級自我創建,並將其添加到框架而不是第二個面板。但是,如果有人仍然想告訴我爲什麼我嘗試的初始解決方案不起作用,我將不勝感激。

1

代碼不起作用的原因是因爲webview窗口小部件不在它自己的sizer中。因此它不知道擴大。如果你將它添加到sizer中,它就可以工作。見下:

import wx 
import wx.html2 

class MainFrame(wx.Frame): 

    def __init__(self, *args, **kwargs): 
     super(MainFrame, self).__init__(*args, **kwargs) 
     self.InitUI() 
     self.Centre() 
     self.Show() 

    def InitUI(self): 
     hbox = wx.BoxSizer(wx.HORIZONTAL) 
     htmlSizer = wx.BoxSizer(wx.VERTICAL) 

     listPanel = wx.Panel(self, -1, style=wx.SUNKEN_BORDER) #This is the panel where the news articles would be shown 
     htmlPanel = wx.Panel(self, -1, style=wx.SUNKEN_BORDER) #This is the panel where the web view would be shown 

     browser = wx.html2.WebView.New(htmlPanel) #I create the new web view here with the htmlPanel as its parent 
     browser.LoadURL("http://www.google.com") #And then I load Google here 
     htmlSizer.Add(browser, 1, wx.EXPAND) 
     htmlPanel.SetSizer(htmlSizer) 

     hbox.Add(listPanel, 1, wx.EXPAND) #Then I add both panels to the frame. Not sure where I went wrong. 
     hbox.Add(htmlPanel, 2, wx.EXPAND) 

     self.SetAutoLayout(True) 
     self.SetSizer(hbox) 
     self.Layout() 

def main(): 
    app = wx.App() 
    frame = MainFrame(None, title='What is this box? HELP!', size=(800,480)) 
    app.MainLoop() 


if __name__ == '__main__': 
    main() 
+0

啊,有道理。謝謝。 – JackSac67