我在wxPython中制作了一个Huffington Post RSS feed聚合器,但是我遇到了一些麻烦。在程序中,主wx.Frame中有两个面板:一个显示所有文章的列表,另一个显示用户选择的文章的Web视图。我还没有完成那部分,所以我决定只通过加载Google测试网页视图小部件。但是当我这样做时,我得到了一些奇怪的结果。以下是相关代码:
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 :(得分: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()
答案 1 :(得分:0)
我似乎已经解决了这个问题。我没有创建面板然后尝试将Web视图放入面板中,而是将Web视图的父视图设为self,并将其添加到框架而不是第二个面板。但是,如果有人仍然想告诉我为什么我尝试的初始解决方案不起作用,我将不胜感激。