2
A
回答
2
一些谷歌搜索显示this链接给我。
我已经包含了那里给出的示例代码,我希望能够对它进行评论,以便您能够理解发生了什么。
我稍微不确定我的for-loop描述的准确性,所以请评论是否有任何错误。
import tkinter as tk
# Define the data points
data = [20, 15, 10, 7, 5, 4, 3, 2, 1, 1, 0]
root = tk.Tk()
root.title("Bar Graph")
c_width = 400 # Define it's width
c_height = 350 # Define it's height
c = tk.Canvas(root, width=c_width, height=c_height, bg='white')
c.pack()
# The variables below size the bar graph
y_stretch = 15 # The highest y = max_data_value * y_stretch
y_gap = 20 # The gap between lower canvas edge and x axis
x_stretch = 10 # Stretch x wide enough to fit the variables
x_width = 20 # The width of the x-axis
x_gap = 20 # The gap between left canvas edge and y axis
# A quick for loop to calculate the rectangle
for x, y in enumerate(data):
# coordinates of each bar
# Bottom left coordinate
x0 = x * x_stretch + x * x_width + x_gap
# Top left coordinates
y0 = c_height - (y * y_stretch + y_gap)
# Bottom right coordinates
x1 = x * x_stretch + x * x_width + x_width + x_gap
# Top right coordinates
y1 = c_height - y_gap
# Draw the bar
c.create_rectangle(x0, y0, x1, y1, fill="red")
# Put the y value above the bar
c.create_text(x0 + 2, y0, anchor=tk.SW, text=str(y))
root.mainloop()
1
最后这个简单的代码我写了,这可能是有点多余的,不是很灵活。
from tkinter import *
root = Tk()
root.title("Bar Graph")
c_width = 500
c_height = 200
c = Canvas(root, width=c_width, height=c_height)
c.pack()
c.create_rectangle(20, 140, 120, 180, fill="red")
c.create_text(70, 130, text="Projects--20%")
c.create_rectangle(140, 160, 240, 180, fill="blue")
c.create_text(190, 150, text="Quizzes--10%")
c.create_rectangle(260, 120, 360, 180, fill="green")
c.create_text(310, 110, text="Midterm--30%")
c.create_rectangle(380, 100, 480, 180, fill="orange")
c.create_text(430, 90, text="Final--40%")
c.create_line(0, 180, 500, 180)
root.mainloop()
相关问题
- 1. 使用Tkinter绘图
- 2. 使用按钮绘制形状(Python Tkinter)
- 3. 我如何使用Tkinter绘制Rombus?
- 4. 使用Tkinter绘制一个矩形?
- 5. 在Tkinter(Python)中绘制圆
- 6. 在Tkinter中绘制Pacman's Face
- 7. Tkinter绘制两个画布
- 8. 绘制矩形tkinter不起作用
- 9. 使用PIL或枕头绘制图像并用TKinter显示它
- 10. 在Pygame中使用Tkinter绘制一个圆圈
- 11. python tkinter canvas功能,尝试使用StringVar -s绘制弧线
- 12. Python:如何在读取.txt时使用tkinter绘制坐标
- 13. 使用tkinter为nltk绘制jupyter笔记本内
- 14. 使用python和tkinter实时绘制串行数据
- 15. 如何使用tkinter在图像上绘制坐标系?
- 16. 在PIL/TKinter中使用绘图
- 17. Python Tkinter:在标签内绘制箭头
- 18. 在Tkinter(Python)中绘制波形
- 19. Tkinter帆布绘制风玫瑰
- 20. 在命令行上绘制tkinter画布
- 21. 用Tkinter处理绘图
- 22. 我想用python中的Tkinter库中的双边绘制矩形?
- 23. 使用Tkinter中的滑块绘制图表 - 程序没有输出
- 24. 如何使用类在Python中的tkinter画布上绘制多边形
- 25. 绘制使用PTB
- 26. 绘制使用pylab
- 27. 使用Tkinter的
- 28. 使用textureRegion的绘制
- 29. 使用drawPoint()的绘制线
- 30. 绘制使用的Gnuplot
你真的写了任何代码吗? – Ellis