gpt4 book ai didi

python - 将直方图放入 tkinter 框架中

转载 作者:行者123 更新时间:2023-12-01 05:10:34 25 4
gpt4 key购买 nike

我正在尝试将 2 个 Tkinter 程序放在一起。从用户输入的 URL 中获取每个字母的所有出现次数,并在文本框中显示每个字母的计数。另一个是创建直方图的turtle模块。我希望将每个字母的出现次数显示为直方图,并在 x 轴上显示字母的标签。

这是我的代码:

from tkinter import *
import tkinter.messagebox
import urllib.request
import turtle

counts = 0

def main():
analyzeFile(url.get())
list = [counts]
drawHistogram(list)

def analyzeFile(url):
try:
infile = urllib.request.urlopen(url)
s = str(infile.read().decode()) # Read the content as string from the URL

counts = countLetters(s.lower())

infile.close() # Close file
except ValueError:
tkinter.messagebox.showwarning("Analyze URL",
"URL " + url + " does not exist")

def countLetters(s):
counts = 26 * [0] # Create and initialize counts
for ch in s:
if ch.isalpha():
counts[ord(ch) - ord('a')] += 1
return counts

def drawHistogram(list):
WIDTH = 400
HEIGHT = 300

raw_turtle.penup()
raw_turtle.goto(-WIDTH / 2, -HEIGHT / 2)
raw_turtle.pendown()
raw_turtle.forward(WIDTH)

widthOfBar = WIDTH / len(list)

for i in range(len(list)):
height = list[i] * HEIGHT / max(list)
drawABar(-WIDTH / 2 + i * widthOfBar,
-HEIGHT / 2, widthOfBar, height)

raw_turtle.hideturtle()

def drawABar(i, j, widthOfBar, height):
raw_turtle.penup()
raw_turtle.goto(i, j)
raw_turtle.setheading(90)
raw_turtle.pendown()

raw_turtle.forward(height)
raw_turtle.right(90)
raw_turtle.forward(widthOfBar)
raw_turtle.right(90)
raw_turtle.forward(height)

window = Tk()
window.title("Occurrence of Letters in a Histogram from URL")

frame1 = Frame(window)
frame1.pack()

scrollbar = Scrollbar(frame1)
scrollbar.pack(side = RIGHT, fill = Y)

canvas = tkinter.Canvas(frame1, width=450, height=450)
raw_turtle = turtle.RawTurtle(canvas)

scrollbar.config(command = canvas.yview)
canvas.config( yscrollcommand=scrollbar.set)
canvas.pack()

frame2 = Frame(window)
frame2.pack()

Label(frame2, text = "Enter a URL: ").pack(side = LEFT)
url = StringVar()
Entry(frame2, width = 50, textvariable = url).pack(side = LEFT)
Button(frame2, text = "Show Result", command = main).pack(side = LEFT)

window.mainloop()

def main():开始是直方图部分。如何组合这些以便直方图显示而不是文本框?预先感谢您的任何意见!

编辑: enter image description here

最佳答案

您可以使用RawTurtle(有关详细信息,请参阅 this 主题)。因此,在代码中定义 raw_turtle:

canvas = tkinter.Canvas(frame1, width=450, height=450)
raw_turtle = turtle.RawTurtle(canvas)

并将其用于直方图。完整代码如下所示:

from tkinter import *
import tkinter.messagebox
import urllib.request
import turtle


def main():
counts = analyzeFile(url.get())
drawHistogram(counts)

def analyzeFile(url):
try:
infile = urllib.request.urlopen(url)
s = str(infile.read().decode()) # Read the content as string from the URL

counts = countLetters(s.lower())

infile.close() # Close file
except ValueError:
tkinter.messagebox.showwarning("Analyze URL",
"URL " + url + " does not exist")

return counts

def countLetters(s):
counts = 26 * [0] # Create and initialize counts
for ch in s:
if ch.isalpha():
counts[ord(ch) - ord('a')] += 1
return counts

def drawHistogram(list):

WIDTH = 400
HEIGHT = 300

raw_turtle.penup()
raw_turtle.goto(-WIDTH / 2, -HEIGHT / 2)
raw_turtle.pendown()
raw_turtle.forward(WIDTH)

widthOfBar = WIDTH / len(list)

for i in range(len(list)):
height = list[i] * HEIGHT / max(list)
drawABar(-WIDTH / 2 + i * widthOfBar,
-HEIGHT / 2, widthOfBar, height, letter_number=i)

raw_turtle.hideturtle()

def drawABar(i, j, widthOfBar, height, letter_number):
alf='abcdefghijklmnopqrstuvwxyz'
raw_turtle.penup()
raw_turtle.goto(i+2, j-20)

#sign letter on histogram
raw_turtle.write(alf[letter_number])
raw_turtle.goto(i, j)

raw_turtle.setheading(90)
raw_turtle.pendown()


raw_turtle.forward(height)
raw_turtle.right(90)
raw_turtle.forward(widthOfBar)
raw_turtle.right(90)
raw_turtle.forward(height)

window = Tk()
window.title("Occurrence of Letters in a Histogram from URL")

frame1 = Frame(window)
frame1.pack()

scrollbar = Scrollbar(frame1)
scrollbar.pack(side = RIGHT, fill = Y)

canvas = tkinter.Canvas(frame1, width=450, height=450)
raw_turtle = turtle.RawTurtle(canvas)

scrollbar.config(command = canvas.yview)
canvas.config( yscrollcommand=scrollbar.set)
canvas.pack()

frame2 = Frame(window)
frame2.pack()

Label(frame2, text = "Enter a URL: ").pack(side = LEFT)
url = StringVar()
Entry(frame2, width = 50, textvariable = url).pack(side = LEFT)
Button(frame2, text = "Show Result", command = main).pack(side = LEFT)

window.mainloop()

This关于在 Canvas 上使用滚动条的主题也可能有用。

编辑
当我运行上面的代码时,我看到: enter image description here

关于python - 将直方图放入 tkinter 框架中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24319683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com