我有一个小程序进入新闻聚合器,获取hrefs,并在窗口中返回。如果选择多个站点,我想打开多个窗口,现在,它只会转到列表中的第一个,并完美地完成。我假设我没有正确地将列表的内容传递给我的程序中的下一步。
import wx
import urllib2
from BeautifulSoup import BeautifulSoup
import re
from pyparsing import makeHTMLTags, originalTextFor, SkipTo, Combine
import wx
import wx.html
global P
siteDict = {0:'http://www.reddit.com', 1:'http://www.digg.com',2:'http://www.stumbleupon.com', 3:'news.google.com'}
class citPanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
allSites = ['Reddit', 'Digg', 'StumbleUpon', 'Google News']
wx.StaticText(self, -1, "Choose the Sites you would like Charlie to Visit:", (45, 15))
self.sitList = wx.CheckListBox(self, 20, (60, 50), wx.DefaultSize, allSites)
class nextButton(wx.Button):
def __init__(self, parent, id, label, pos):
wx.Button.__init__(self, parent, id, label, pos)
class checkList(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(400, 300))
self.panel = citPanel(self, -1)
nextButton(self.panel, 30, 'Ok', (275, 50))
self.Bind(wx.EVT_BUTTON, self.Clicked)
self.Centre()
self.Show(True)
def Clicked(self, event):
checkedItems = [i for i in range(self.panel.sitList.GetCount()) if self.panel.sitList.IsChecked(i)]
print checkedItems
r = [siteDict[k] for k in checkedItems]
print r
for each in r:
pre = '<HTML><head><title>Page title</title></head>'
post = '</HTML>'
site = urllib2.urlopen(each)
html=site.read()
soup = BeautifulSoup(html)
tags = soup.findAll('a')
soup1 = BeautifulSoup(''.join(str(t) for t in tags))
print soup1.prettify()
aTag,aEnd = makeHTMLTags("A")
aTag = originalTextFor(aTag)
aEnd = originalTextFor(aEnd)
aLink = aTag + SkipTo(aEnd) + aEnd
aLink.setParseAction(lambda tokens : ''.join(tokens))
links = aLink.searchString(html)
out = []
out.append(pre)
out.extend([' '+lnk[0] for lnk in links])
out.append(post)
P= '\n'.join(out)
print type(P)
print P
class MyHtmlFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title)
html = wx.html.HtmlWindow(self)
if "gtk2" in wx.PlatformInfo:
html.SetStandardFonts()
html.SetPage(P)
app = wx.PySimpleApp()
frm = MyHtmlFrame(None, "Charlie")
frm.Show()
app.MainLoop()
#event.Skip()
#self.Destroy()
app = wx.App()
checkList(None, -1, 'Charlie')
app.MainLoop()
我有一些混合的打印语句用于调试。所以,任何帮助,非常感谢。而且,如果他们都在同一个窗口结束,那也没关系。只是试图超越唯一做第一个
答案 0 :(得分:1)
Eeek!您正在定义MyHTMLFrame类-inside-一个事件处理函数(不是一个好主意)。
我无法运行脚本,因为我没有模块PyParsing(总是生成尽量少依赖的样本......)
因此,我不确定这段代码是否运行,但它应该给你一般的想法。这是修改过的代码,我在clicked()函数之后只改了一下
def Clicked(self, event):
checkedItems = [i for i in range(self.panel.sitList.GetCount()) if self.panel.sitList.IsChecked(i)]
print checkedItems
r = [siteDict[k] for k in checkedItems]
print r
for each in r:
pre = '<HTML><head><title>Page title</title></head>'
post = '</HTML>'
site = urllib2.urlopen(each)
html=site.read()
soup = BeautifulSoup(html)
tags = soup.findAll('a')
soup1 = BeautifulSoup(''.join(str(t) for t in tags))
print soup1.prettify()
aTag,aEnd = makeHTMLTags("A")
aTag = originalTextFor(aTag)
aEnd = originalTextFor(aEnd)
aLink = aTag + SkipTo(aEnd) + aEnd
aLink.setParseAction(lambda tokens : ''.join(tokens))
links = aLink.searchString(html)
out = []
out.append(pre)
out.extend([' '+lnk[0] for lnk in links])
out.append(post)
P= '\n'.join(out)
print type(P)
print P
# create the html frame, pass it in your string
frm = MyHtmlFrame(None, "Charlie", P)
frm.Show()
class MyHtmlFrame(wx.Frame):
def __init__(self, parent, title, page): # pass it in the page variable
wx.Frame.__init__(self, parent, -1, title)
html = wx.html.HtmlWindow(self)
if "gtk2" in wx.PlatformInfo:
html.SetStandardFonts()
html.SetPage(page)
app = wx.App()
checkList(None, -1, 'Charlie')
checkList.Show() # show first frame, which opens other windows
app.MainLoop()
呃我讨厌用4个空格缩进来在StackOverflow中启动代码。希望这对你有用!
答案 1 :(得分:0)
我对WxPython并不熟悉,但似乎有两个MainLoop
正在运行
当我注释掉两行时,我让你的程序打开两个窗口,如下所示:
class MyHtmlFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title)
html = wx.html.HtmlWindow(self)
if "gtk2" in wx.PlatformInfo:
html.SetStandardFonts()
html.SetPage(P)
#app = wx.PySimpleApp()
frm = MyHtmlFrame(None, "Charlie")
frm.Show()
#app.MainLoop()