以下程序要求用户选择一个文件,对文件中的字符(a-z)进行计数,并在直方图中显示字符计数的结果。如何获得每个直方图栏下方的字符(a-z)?
我是否需要创建另一个框架,还是可以简单地在canvas.create_text区域中更改某些内容?
这是完整的代码:
from tkinter import * # Import tkinter
import tkinter.messagebox # Import tkinter.messagebox
from tkinter.filedialog import askopenfilename
def showResult():
analyzeFile(filename.get())
def analyzeFile(filename):
try:
infile = open(filename, "r") # Open the file
counts = 26 * [0] # Create and initialize counts
for line in infile:
# Invoke the countLetters function to count each letter
countLetters(line.lower(), counts)
# Display the results in a histogram
bottomGap = 10
canvas.delete("line")
canvas.create_line(10, height - bottomGap, width - 10, height - bottomGap, tag="line")
barWidth = (width - 20) / len(counts)
maxCount = int(max(counts))
for i in range(len(counts)):
canvas.create_rectangle(i * barWidth + 10, (height - bottomGap) * (1 - counts[i] / (maxCount + 4)),
(i + 1) * barWidth + 10, height - bottomGap, tag="line")
canvas.create_text(i * barWidth + 10 + barWidth / 2,
(height - bottomGap) * (1 - counts[i] / (maxCount + 4)) - 8,
text=str(counts[i]), tag="line")
infile.close() # Close file
except IOError:
tkinter.messagebox.showwarning("Analyze File",
"File " + filename + " does not exist")
# Count each letter in the string
def countLetters(line, counts):
for ch in line:
if ch.isalpha():
counts[ord(ch) - ord('a')] += 1
def openFile():
filenameforReading = askopenfilename()
filename.set(filenameforReading)
window = Tk() # Create a window
window.title("Occurrence of Letters") # Set title
frame1 = Frame(window) # Hold four labels for displaying cards
frame1.pack()
# canvas for the histogram display
width = 340
height = 150
radius = 2
canvas = Canvas(window, width=width, height=height)
canvas.pack()
frame2 = Frame(window) # Hold four labels for displaying cards
frame2.pack()
Label(frame2, text="Enter a filename: ").pack(side=LEFT)
filename = StringVar()
Entry(frame2, width=20, textvariable=filename).pack(side=LEFT)
Button(frame2, text="Browse", command=openFile).pack(side=LEFT)
Button(frame2, text="Show Result", command=showResult).pack(side=LEFT)
window.mainloop() # Create an event loop
答案 0 :(得分:0)
我正在回答自己想问的任何人的问题。我只是更改了文本的位置以及文本将是什么:
原始代码:
canvas.create_text(i * barWidth + 10 + barWidth / 2,
(height - bottomGap) * (1 - counts[i] / (maxCount + 4)) - 8,
text=str(counts[i]), tag="line")
固定代码:
canvas.create_text(i * barWidth + 10 + barWidth / 2,
height - bottomGap / 4,
text=str(chr(ord('a') + i)), tag="line")
如果有人要添加任何内容,我全都在学习!