选择文件用直方图表示字母出现次数

模板1:需要输入地址

在这里插入图片描述
源码如下:

from tkinter import *
import random

height = 660
width = 800

def findMaxValue(list):
    maxValue =int( list[0][0] )
    for column in range(1, len(list)-1):
        if int(list[column][0]) > maxValue:
             maxValue = int(list[column][0])

    return maxValue

class RectImage():
    def __init__(self, mywin, list):    # 2维的列表 [0]:数字,[1]字符
        self.win = mywin
        self.list = list
        listLength = len(self.list)
        self.barwidth = (width - 20) // listLength
        self.height = height // findMaxValue(self.list)
        self.left = (width - listLength * self.barwidth) // 2
        self.drawRect()

    def drawRect(self):  # 2维的列表 [0]:数字,[1]字符
        self.win.canvas.delete("line")
        for i in range(len(self.list)):
            color = "Blue"
            self.win.canvas.create_rectangle(i * self.barwidth + self.left,
                                             height - 30,
                                             (i + 1) * self.barwidth + self.left,
                                             height - 20 - self.height * self.list[i][0],
                                             fill=color, tag="line")

            self.win.canvas.create_text(i * self.barwidth + self.left + self.barwidth / 2,
                                        height - 20,
                                        text=str(self.list[i][1]), tag="line")

            self.win.canvas.create_text(i * self.barwidth + self.left + self.barwidth / 2,
                                        height - 25  - self.height * self.list[i][0],
                                        text=str(self.list[i][0]), tag="line")

class BrowserGUI:
    def __init__(self):
        self.window = Tk()
        self.window.title("Occurrence of Letters Histogram")
        self.window.canvas = Canvas(self.window, bg="white", width=width, height=height)
        self.window.canvas.pack()
        frame1 = Frame(self.window)
        frame1.pack()
        Label(frame1, text="Enter a element:").grid(row=1, column=1, sticky=W)
        self.filenameVar = StringVar()
        Entry(frame1, width=40,textvariable=self.filenameVar).grid(row=1, column=2)
        Button(frame1, text="Browser", command=self.drawShowNumber).grid(row=1, column=3)
        self.window.mainloop()

    def drawShowNumber(self):
        filename = self.filenameVar.get().strip()  # C:\Users\Lenovo\Desktop\1.txt
        wordCounts = self.open(filename)

        lst = list(wordCounts.items())
        lst.sort()
        newLst = [[x, y] for (y, x) in lst]  # 2维的列表 [0]:数字,[1]字符
        RectImage(self.window, newLst)

    def open(self, filename):
        infile = open(filename, "r")
        wordCounts = {}
        ch = infile.read(1)
        while ch != '':
            self.processCount(ch.lower(), wordCounts)
            ch = infile.read(1)
        infile.close()
        return wordCounts

    def processCount(self, line, wordCounts):
        line = self.replacePunctuation(line)
        words = line.split()  # 列表
        for word in words:
            if word in wordCounts:
                wordCounts[word] += 1
            else:
                wordCounts[word] = 1

    # replace punctuation in the line with space
    def replacePunctuation(self, line):
        for ch in line:
            if ch in "~!@#$%^&*()_+{}[]|\"';:?/>.<,":
                line = line.replace(ch, " ")
        return line


BrowserGUI()

模板2:直接打开选择文件

在这里插入图片描述

from tkinter import *
from tkinter.filedialog import askopenfilename
import random
import os.path
import sys

height = 660
width = 800


def findMaxValue(list):
    maxValue = int(list[0][0])
    for column in range(1, len(list) - 1):
        if int(list[column][0]) > maxValue:
            maxValue = int(list[column][0])

    return maxValue


class RectImage():
    def __init__(self, mywin, list):  # 2维的列表 [0]:数字,[1]字符
        self.win = mywin
        self.list = list
        listLength = len(self.list)
        self.barwidth = (width - 20) // listLength
        self.height = height // findMaxValue(self.list)
        self.left = (width - listLength * self.barwidth) // 2
        self.drawRect()

    def drawRect(self):  # 2维的列表 [0]:数字,[1]字符
        self.win.canvas.delete("line")
        for i in range(len(self.list)):
            color = "Blue"
            self.win.canvas.create_rectangle(i * self.barwidth + self.left,
                                             height - 30,
                                             (i + 1) * self.barwidth + self.left,
                                             height - 20 - self.height * self.list[i][0],
                                             fill=color, tag="line")

            self.win.canvas.create_text(i * self.barwidth + self.left + self.barwidth / 2,
                                        height - 20,
                                        text=str(self.list[i][1]), tag="line")

            self.win.canvas.create_text(i * self.barwidth + self.left + self.barwidth / 2,
                                        height - 25 - self.height * self.list[i][0],
                                        text=str(self.list[i][0]), tag="line")


class BrowserGUI:
    def __init__(self):
        self.window = Tk()
        self.window.title("Occurrence of Letters Histogram")
        self.window.canvas = Canvas(self.window, bg="white", width=width, height=height)
        self.window.canvas.pack()
        frame1 = Frame(self.window)
        frame1.pack()
        Label(frame1, text="Select a fileName:").grid(row=1, column=1, sticky=W)
        Button(frame1, text="Browser", command=self.selectText).grid(row=1, column=2)
        Button(frame1, text="Show Result", command=self.drawShowNumber).grid(row=1, column=3)
        self.window.mainloop()

    def selectText(self):
        self.filenameforReading = askopenfilename()

    def drawShowNumber(self):
        wordCounts = self.open(self.filenameforReading)
        lst = list(wordCounts.items())
        lst.sort()
        newLst = [[x, y] for (y, x) in lst]  # 2维的列表 [0]:数字,[1]字符
        RectImage(self.window, newLst)

    def open(self, filename):
        if not os.path.isfile(filename):
            print(filename + "doesn't exit")
            sys.exit()
        infile = open(filename, "r")
        wordCounts = {}
        ch = infile.read(1)
        while ch != '':
            self.processCount(ch.lower(), wordCounts)
            ch = infile.read(1)
        infile.close()
        return wordCounts

    def processCount(self, line, wordCounts):
        line = self.replacePunctuation(line)
        words = line.split()  # 列表
        for word in words:
            if word in wordCounts:
                wordCounts[word] += 1
            else:
                wordCounts[word] = 1

    # replace punctuation in the line with space
    def replacePunctuation(self, line):
        for ch in line:
            if ch in "~!@#$%^&*()_+{}[]|\"';:?/>.<,":
                line = line.replace(ch, " ")
        return line


BrowserGUI()

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

广大菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值