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