玩蛇网提供最新Python编程技术信息以及Python资源下载!

python rss框架内部做添加数据操作遇到的问题

import tkinter
import urllib
import xml.parsers.expat
class MyXML:                                    
    def __init__(self, edit):
        self.parser = xml.parsers.expat.ParserCreate()            
        self.parser.StartElementHandler = self.start            
        self.parser.EndElementHandler = self.end            
        self.parser.CharacterDataHandler = self.data            
        self.title = False                        
        self.description = False
        self.date = False
        self.edit = edit                        
    def start(self, name, attrs):                       
        if name == 'title':                        
            self.title = True                    
        elif name == 'description':
            self.description = True
        elif name == 'pubDate':                        
            self.date = True                    
        else:
            pass
    def end(self, name):                            
        if name == 'title':
            self.title = False                    
        elif name == 'description':
            self.description = False
        elif name == 'pubDate':
            self.date = False                    
        else:
            pass
    def data(self,data):                            
        if self.title:                            
            self.edit.insert(tkinter.END,
                    '******************************\n')
            self.edit.insert(tkinter.END, 'Title: ')
            self.edit.insert(tkinter.END, data + '\n')
        elif self.description:
            self.edit.insert(tkinter.END, 'Date: ')
            self.edit.insert(tkinter.END, data + '\n')
        elif self.date:
            self.edit.insert(tkinter.END, 'Date: ')
            self.edit.insert(tkinter.END, data + '\n')
        else:
            pass
    def feed(self, data):
        self.parser.Parse(data, 0)
class Window:
    def __init__(self, root):
        self.root = root                            
        self.entryUrl = tkinter.Entry(root,width = 30)
        self.entryUrl.place(x = 65, y = 15)
        self.get = tkinter.Button(root,
                text = '读取RSS', command = self.Get, font = ('system','10'))
        self.get.place(x = 350, y = 15)
        self.frame = tkinter.Frame(root, bd=2)
        self.scrollbar = tkinter.Scrollbar(self.frame)
        self.edit = tkinter.Text(self.frame,yscrollcommand = self.scrollbar.set,
                width = 96, height = 32)
        self.scrollbar.config(command=self.edit.yview)
        self.edit.pack(side = tkinter.LEFT)
        self.scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
        self.frame.place(y = 50)
    def Get(self):
        url = self.entryUrl.get()
        page = urllib.urlopen(url)                        
        data = page.read()                            
        parser = MyXML(self.edit)                        
        parser.feed(data)                            
        page.close()
root = tkinter.Tk()
root.title('RSS 阅读器')
root.minsize(700,500)
root.maxsize(700,500)
root.mainloop()

粘贴自互联网,这个做出来只有一个GUI视图,如何在里面添加东西

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd20489.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年03月23日 - 14时37分16秒 发布自玩蛇网

您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容
我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules