2010-01-13 4 views
0

뉴스 집계 도구로 이동하고, hrefs를 가져 와서 창에 반환하는 작은 프로그램이 있습니다. 여러 사이트가 선택되면 여러 창이 열리길 원합니다. 지금은 목록의 첫 번째 사이트로 이동하여 완벽하게 완료됩니다. 내 프로그램의 다음 단계로 제대로 목록의 내용을 전달하지 않는다고 가정합니다.WxPython의 목록에서 여러 창 열기

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

나는 디버깅을 위해 혼합 일부 인쇄 문이있다. 그래서, 어떤 도움을 많이 주셔서 감사합니다. 또한, 그들 모두가 같은 창에서 끝나면 그것은 괜찮을 것입니다. 첫 번째 작업 만 수행하려고 시도합니다.

답변

1

Eeek를 아래 그림과 같이
는 내가 두 줄을 주석 처리 할 때 두 개의 창을 열 수있는 프로그램을 가지고! MyHTMLFrame 클래스를 정의하고 있습니다. 이벤트 처리 함수 (좋은 생각은 아닙니다). 내가 모듈 대한 파싱을하지 않는

나는 코드 실행이 경우

따라서, 나는 확실하지 않다 (항상 ... 가능한 한 적은 종속성이 샘플을 만들 수) 스크립트를 실행할 수 없습니다 ,하지만 그것은 당신에게 일반적인 아이디어를 제공해야합니다. I에 유래 코드를 시작 4 공백으로 압입 싫어 우 여기서 코드 수정, I은 클릭 된() 함수

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

후에 약간 변경했습니다. 희망이 당신을 위해 작동합니다!

+0

4 칸에 동의합니다. 그것은 그것을하는 것처럼 보였다. 나는 모든 것에 익숙하지 않기 때문에 문법과 일을하는 비단뱀 방식에 관한 벽에 부딪히는 경향이 있습니다. 덕분에 – Kevin

+0

도와 줘서 기쁩니다. 나는 wx에 익숙해 질 수있는 가장 좋은 방법을 찾았습니다. 점점 더 놀아서 결국 모든 것이 "클릭"될 것입니다. :) –

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

각 사이트에서 하나를 열었습니까? 두 줄을 주석 처리 했음에도 불구하고 원래 창, wxpython stdout/stdin 창 및 첫 번째 확인 된 사이트가 있습니다. 두 개를 선택해도 한 개만 표시됩니다. – Kevin

+0

예, 각 사이트마다 창을 엽니 다. –