美文网首页
初探Tkinter的用法,搭出一张不好看的脸

初探Tkinter的用法,搭出一张不好看的脸

作者: Hellooooooworld | 来源:发表于2017-05-31 19:50 被阅读248次

    人靠衣装马靠鞍,狗带铃铛跑得欢!

    我们在Python中一个劲的码代码的时候,我们会想,怎么样的界面才能和我的功能相对应。于是我也因为这样的想法,接触了Tkinter。。然后一个丑出境界的界面诞生了,但是因为是我第一个使用Tkinter搭建的界面,所以还是和大家一起分享一下。
    我实现的功能包括进行分词,词性标注,分词拼音,词频统计,字频统计等小功能。

    part.0 包的安装和引用

    0.1 界面的引用
    # -*- coding: utf-8 -*-
    from Tkinter import *
    import thing  #thing 是我自己写的函数py文件
    
    0.2函数文件的引用项
    # -*- coding: utf-8 -*-
    import jieba
    import jieba.posseg as pseg
    import pinyin
    from nltk import FreqDist
    

    part.1 thing文件函数撰写

    这部分的函数的使用方法,在我之前的一篇简书中已经详细提及。
    附上传送门--->结巴分词和NLTK----一套中文文本分析的组合拳

    def wordcut(content):
        wordlist=list(jieba.cut(content))
        return " ".join(wordlist)
    
    def wordpseg(content):
        words=pseg.cut(content)
        wordslist=[]
        for wds in words:
            if wds.word!=" ":
                wordslist.append(wds.word+"/"+wds.flag)
        return "  ".join(wordslist)
    
    def wordpinyin(content):
        content=wordcut(content)
        return pinyin.get_pinyin(content)
    
    #字频
    def Freq(content):
        wordlist=[]
        numlist=[]
        Frelist=[]
        kl=[]
        Sum=0
        wordfr=FreqDist(wordcut(content))
        wordfr=sort_item(wordfr.items())
        for i,j in wordfr:
            if not i==" ":
                wordlist.append(i)
                numlist.append(j)
                Sum=Sum+j
        for s in numlist:
            Frelist.append(float(s)/Sum)
        lastlist=[wordlist,numlist,Frelist]
        for i in range(len(lastlist[0])):
            kl.append("%s\t\t\t\t\t\t%d\t\t\t\t\t\t%.3f"%(lastlist[0][i],lastlist[1][i],lastlist[2][i]))
        return kl
    
    #词频
    def Freqc(content):
        wordlist = []
        numlist = []
        Frelist = []
        kl = []
        Sum = 0
        wordfr=FreqDist(list(jieba.cut(content)))
        wordfr = sort_item(wordfr.items())
        for i, j in wordfr:
            if not i == " ":
                wordlist.append(i)
                numlist.append(j)
                Sum = Sum + j
        for s in numlist:
            Frelist.append(float(s) / Sum)
        lastlist = [wordlist, numlist, Frelist]
        for i in range(len(lastlist[0])):
            kl.append("%s\t\t\t\t\t\t%d\t\t\t\t\t\t%.3f" % (lastlist[0][i], lastlist[1][i], lastlist[2][i]))
        return kl
    
    def sort_item(item):#排序函数,正序排序
        vocab=[]
        for k,v in item:
            vocab.append((k,v))
        List=list(sorted(vocab,key=lambda v:v[1],reverse=1))
        return List
    

    part.2 Tkinter 界面

    我一共写了4个界面,一个索引界面和三个功能界面,在代码中,我已经做了部分的注释。因为界面部分功能的相似性,所以我对相似的部分简单介绍或省略。
    main()是索引界面,cutandpseg()模块主要实现的是对长句的分词和词性标注,pinyinandcut()是对长句进行分词和拼音标注,Frelist()则是显示字频和词频。

    class Chinese():
        def main(self):
            self.mainroot=Tk()
            self.mainroot.title("选择")
            self.mainroot.geometry("200x300")
            self.mainroot.resizable(width=False,height=False)
            self.chooseFrame=Frame(self.mainroot)
            self.chooseFrame.pack()
            self.Buttoncut=Button(self.chooseFrame,text="分词",command=self.gotocut)
            self.Buttonpy=Button(self.chooseFrame,text="拼音",command=self.gotopinyin)
            self.Buttonsum=Button(self.chooseFrame,text="词频统计",command=self.gotoFre)
            self.Buttoncut.pack()
            self.Buttonpy.pack()
            self.Buttonsum.pack()
            self.mainroot.mainloop()
        def cutandpseg(self):
            self.root=Tk()
            self.root.title("分词器")
            self.root.geometry("800x400")
            self.root.resizable(width=True,height=False)
            self.mainFrame=Frame(self.root)
            self.mainFrame.pack()
            self.backButton=Button(self.mainFrame,text="返回",command=self.cutbackmain)
            self.backButton.pack(side=TOP,anchor=W)
            self.backButton.pack()
            # 第一个label标签
            self.firstlabel = Label(self.mainFrame, text="输入框")
            self.firstlabel.pack()
            # 输入框
            self.Inputtext = Text(self.mainFrame, height=7)
            self.Inputtext.insert(INSERT, "输入一段文字,点击“分词和词性标注”按钮,显示汉语分词和词性自动标注结果。")
            self.Inputtext.pack()
            # 第二个label标签
            self.secondlabel = Label(self.mainFrame, text="结果")
            self.secondlabel.pack()
            # 输出框
            self.Outputtext = Text(self.mainFrame, height=7)
            self.Outputtext.pack()
            # 按键
            self.frameButton = Frame(self.root)
            self.CutButton = Button(self.frameButton, text="分词", command=self.cut)
            self.PsegButton = Button(self.frameButton, text="标注词性", command=self.pseg)
            self.CutButton.pack(side=LEFT, pady=5)
            self.PsegButton.pack(side=LEFT, padx=5)
            self.CutButton.pack()
            self.PsegButton.pack()
            self.frameButton.pack()
            self.root.mainloop()
        def pinyinandcut(self):
            self.pinyinroot=Tk()
            self.pinyinroot.title("拼音转换器")
            self.pinyinroot.geometry("800x400")
            self.pinyinroot.resizable(width=True, height=False)
            self.pinyinFrame = Frame(self.pinyinroot)
            self.pinyinFrame.pack()
            self.backButton = Button(self.pinyinFrame, text="返回", command=self.pinyinbackmain)
            self.backButton.pack(side=TOP, anchor=W)
            self.backButton.pack()
            # 第一个label标签
            self.firstlabel = Label(self.pinyinFrame, text="输入框")
            self.firstlabel.pack()
            # 输入框
            self.Inputtext = Text(self.pinyinFrame, height=7)
            self.Inputtext.insert(INSERT, "输入一段文字,点击“分词和词性标注”按钮,显示汉语分词和词性自动标注结果。")
            self.Inputtext.pack()
            # 第二个label标签
            self.secondlabel = Label(self.pinyinFrame, text="结果")
            self.secondlabel.pack()
            # 输出框
            self.Outputtext = Text(self.pinyinFrame, height=7)
            self.Outputtext.pack()
            self.frameButton = Frame(self.pinyinroot)
            self.pinyinButton = Button(self.frameButton, text="拼音标注", command=self.pinyin)
            self.pinyinButton.pack(side=LEFT, pady=5)
            self.pinyinButton.pack()
            self.frameButton.pack()
            self.pinyinroot.mainloop()
        def Frelist(self):
            self.freroot = Tk()
            self.freroot.title("词频统计器")
            self.freroot.geometry("800x400")
            self.freroot.resizable(width=True, height=False)
            self.FreFrame = Frame(self.freroot)
            self.FreFrame.pack()
            self.backButton = Button(self.FreFrame, text="返回", command=self.frebacmain)
            self.backButton.pack(side=TOP, anchor=W)
            self.backButton.pack()
            # 第一个label标签
            self.firstlabel = Label(self.FreFrame, text="输入框")
            self.firstlabel.pack()
            # 输入框
            self.Inputtext = Text(self.FreFrame, height=7)
            self.Inputtext.insert(INSERT, "输入一段文字,点击“分词和词性标注”按钮,显示汉语分词和词性自动标注结果。")
            self.Inputtext.pack()
            self.frameButton = Frame(self.freroot)
            self.ziFreButton = Button(self.frameButton, text="字频",command=self.insertziFre)
            self.ciFreButton = Button(self.frameButton, text="词频",command=self.insertciFre)
            self.ziFreButton.pack(side=LEFT, pady=5)
            self.ciFreButton.pack(side=LEFT,padx=5)
            self.ziFreButton.pack()
            self.ciFreButton.pack()
            self.lb=Listbox(self.freroot,selectmode=MULTIPLE,width=300)
            self.lb.pack()
    
    
            self.frameButton.pack()
            self.freroot.mainloop()
    
    
    ###cut,pseg,pinyin 三个函数的功能差不多,就是利用读取输入框内的信息,
    ###进行处理后再放入输出框内,因为分词和词性标注功能在同一个界面,所以我们在每次输出之间都要清空输出框
        def cut(self):
            self.Outputtext.delete("0.0", "end")#先清空输出text框中原有的文字
            textall =self.Inputtext.get("0.0", "end")#读取输入框中的所有文字
            newtext = thing.wordcut(textall)#将读取的文字进行函数操作,并返回结果
            self.Outputtext.insert("0.0", newtext)#将得到的结果,在输出框内显示,从而实现功能
    
        def pseg(self):
            self.Outputtext.delete("0.0", "end")
            textall = self.Inputtext.get("0.0", "end")
            newtext = thing.wordpseg(textall)
            self.Outputtext.insert("0.0", newtext)
        def pinyin(self):
            self.Outputtext.delete("0.0", "end")
            textall = self.Inputtext.get("0.0", "end")
            newtext = thing.wordpinyin(textall)
            self.Outputtext.insert("0.0", newtext)
    
    ###在这部分的函数中,withdraw是窗口消失,deiconify是窗口出现,从而实现页面的跳转
        def gotocut(self):
            self.mainroot.withdraw()
            self.cutandpseg().root.deiconify()
        def gotopinyin(self):
            self.mainroot.withdraw()
            self.pinyinandcut().pinyinroot.deiconify()
        def gotoFre(self):
            self.mainroot.withdraw()
            self.Frelist().freroot.deiconify()
        def cutbackmain(self):
            self.root.withdraw()
            self.main().mainroot.deiconify()
        def pinyinbackmain(self):
            self.pinyinroot.withdraw()
            self.main().mainroot.deiconify()
        def frebacmain(self):
            self.freroot.withdraw()
            self.main().mainroot.deiconify()
    ###显示字词频的时候,我们使用就是listbox控件,因为字频和词频在同一个界面,所以还是得在显示之前,清空原来的数据。
        def insertziFre(self):
            if self.lb:#当listbox不为空的时候,清空所有的数据
                self.lb.delete(0, END)
            textall = self.Inputtext.get("0.0", "end")#读取输入框内的数据
            newlist=thing.Freq(textall)#进行字频的操作,并返回之列表
            for item in newlist:#在listbox中输出
                self.lb.insert(END,item)
        def insertciFre(self):
            if self.lb:
                self.lb.delete(0, END)
            textall = self.Inputtext.get("0.0", "end")
            newlist = thing.Freqc(textall)
            for item in newlist:
                self.lb.insert(END, item)
    
    Chinese().main()
    

    part.3 结果展示

    分词.png 词性标注.png 拼音.png 字频.png 词频.png

    part.4 小结

    每个程序的内在很重要,但是门面工作同样重要,我在面对门面工作的时候,往往都是手足无措,所以,及时的补充界面的搭建知识变得格外重要。

    相关文章

      网友评论

          本文标题:初探Tkinter的用法,搭出一张不好看的脸

          本文链接:https://www.haomeiwen.com/subject/ziljfxtx.html