Tkinter 按钮不显示
Tkinter buttons not showing up
该程序应该让用户 select 一个文件并在直方图中显示出现次数。单击“显示结果”按钮会在文本小部件中显示结果。如果文件不存在,您需要在消息框中显示一条消息。
Image 1 - from Textbook
按钮和其他对象似乎没有显示,如下图所示。
Image 2 - my Screenshot
这是我的代码:
from tkinter import *
import tkinter.messagebox
from tkinter.filedialog import askopenfilename
def showResult():
analyzeFile(filename.get())
def analyzeFile(filename):
try:
infile= open(filename, "r")
letterCount= 26*[0]
for line in infile:
countLetters(line.lower(), letterCount)
infile.close()
except IOError:
tkinter.messagebox.showwarning("Analyze File", "File " + filename + " does not exist")
drawHistogram(letterCount)
def countLetters(line, letterCount):
for chr in line:
if chr.isalpha():
letterCount[ord(chr) - ord('a')] +=1
def openFile():
fileForReading= askopenfilename()
filename.set(fileForReading)
def drawHistogram(count):
canvas.delete("bar")
wide = 400
high = 400
canvas.create_line(0, high-15,wide , high-15)
barWidth= (wide-20) / len(count)
unitHeight= (high-20) /max(count)
for i in range(len(count)):
height= count[i] * unitHeight
canvas.create_rectangle(i*barWidth+10, high-height-15,(i+1)* barWidth+10, high-15, tags = "bar")
canvas.create_text((i+1)*barWidth,high-5, text= chr(i + ord('a')),tags="bar")
window = Tk()
window.title("Occurrence of Letters Histogram")
size= 400
canvas= Canvas(window, width = size, height = size)
canvas.pack()
frame2= Frame(window)
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()
你从来没有打包过frame2
。如果你打包它,那么一切都会出现。另外注意一定要单独一行调用pack语句,以免变量为None
,因为pack
returns没有值
该程序应该让用户 select 一个文件并在直方图中显示出现次数。单击“显示结果”按钮会在文本小部件中显示结果。如果文件不存在,您需要在消息框中显示一条消息。
Image 1 - from Textbook
按钮和其他对象似乎没有显示,如下图所示。
Image 2 - my Screenshot
这是我的代码:
from tkinter import *
import tkinter.messagebox
from tkinter.filedialog import askopenfilename
def showResult():
analyzeFile(filename.get())
def analyzeFile(filename):
try:
infile= open(filename, "r")
letterCount= 26*[0]
for line in infile:
countLetters(line.lower(), letterCount)
infile.close()
except IOError:
tkinter.messagebox.showwarning("Analyze File", "File " + filename + " does not exist")
drawHistogram(letterCount)
def countLetters(line, letterCount):
for chr in line:
if chr.isalpha():
letterCount[ord(chr) - ord('a')] +=1
def openFile():
fileForReading= askopenfilename()
filename.set(fileForReading)
def drawHistogram(count):
canvas.delete("bar")
wide = 400
high = 400
canvas.create_line(0, high-15,wide , high-15)
barWidth= (wide-20) / len(count)
unitHeight= (high-20) /max(count)
for i in range(len(count)):
height= count[i] * unitHeight
canvas.create_rectangle(i*barWidth+10, high-height-15,(i+1)* barWidth+10, high-15, tags = "bar")
canvas.create_text((i+1)*barWidth,high-5, text= chr(i + ord('a')),tags="bar")
window = Tk()
window.title("Occurrence of Letters Histogram")
size= 400
canvas= Canvas(window, width = size, height = size)
canvas.pack()
frame2= Frame(window)
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()
你从来没有打包过frame2
。如果你打包它,那么一切都会出现。另外注意一定要单独一行调用pack语句,以免变量为None
,因为pack
returns没有值