在 WxPython 中从列表中打开多个窗口 [英] Opening multiple windows from a list in WxPython

查看:28
本文介绍了在 WxPython 中从列表中打开多个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序可以访问新闻聚合器,获取hrefs,然后在一个窗口中返回.如果选择多个站点,我想打开多个窗口,现在,它只会转到列表中的第一个,并且完美完成.我假设我没有正确地将列表的内容传递给程序的下一步.

I have a little program that goes to news aggregater's, gets the hrefs, and returns in a window. I want to have multiple windows open if multiple sites are choosen, right now, it will only go to the first one in a list, and completes perfectly. I assume I am not passing the the contents of the list properly to the next step in my program.

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

我混入了一些用于调试的打印语句.所以,任何帮助,非常感谢.此外,如果他们都在同一个窗口结束,那也没关系.只是想通过唯一做第一个

I have some print statements mixed in for debugging. So, any help, much appreciated. Also, if they all ended up on the same window, that would be fine too. Just trying to get past the only doing the first one

推荐答案

Eeek!您正在定义 MyHTMLFrame 类 - 内部 - 一个事件处理函数(不是一个好主意).

Eeek! You're defining your MyHTMLFrame class -inside- an event handler function (not a good idea).

我无法运行脚本,因为我没有 PyParsing 模块(总是制作具有尽可能少依赖关系的示例......)

I can't run the script as I don't have the module PyParsing (always make samples that have as little dependencies as possible...)

因此,我不确定这段代码是否能运行,但它应该能让您大致了解一下.这是修改后的代码,我只是在 clicked() 函数之后做了一点改动

Therefore, I'm not sure if this code runs, but it should give you the general idea. Here is the code modified, I've only changed a bit after the clicked() function

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

我讨厌在 StackOverflow 中缩进 4 个空格来启动代码.希望这对你有用!

ugh I hate indenting by 4 spaces to start code in StackOverflow. Hope this works for you!

这篇关于在 WxPython 中从列表中打开多个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆