gpt4 book ai didi

python - 从 wxPython WebView 得到奇怪的结果

转载 作者:太空宇宙 更新时间:2023-11-03 18:51:16 24 4
gpt4 key购买 nike

我正在 wxPython 中制作一个赫芬顿邮报 RSS 提要聚合器,但我遇到了一些麻烦。在程序中,主 wx.Frame 中有两个面板:一个显示所有文章的列表,另一个显示用户选择的文章的 Web View 。我还没有到达那部分,所以我决定通过加载 Google 来测试 Web View 小部件。然而,当我这样做时,我得到了一些奇怪的结果。相关代码如下:

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

这是我得到的图片:

/image/Go3hS.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()

最佳答案

代码不起作用的原因是 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()

关于python - 从 wxPython WebView 得到奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18321571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com