我已经在tkinter画布中编码了条形图,并且它正在从列表中获取数据。但是,当我更新列表时,条形图不会更新。我怎样才能解决这个问题?
我的代码如下所示:
import tkinter as tk
import random
data = [20, 15, 10, 7, 5, 4, 3, 2, 1, 1, 0]
root = tk.Tk()
root.title("Bar Graph")
c_width = 600 # Define it's width
c_height = 400 # 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()
def update():
count = 0
while count < 5:
barChartData.append(random.randint(1, 11))
count = count + 1
update()
update()
我怎样才能解决这个问题?
每当更改数字时,您将需要删除旧图并创建新图。
您应该通过移动代码以将条形图绘制为函数来执行此操作。
def draw_barchart(data):
c.delete("all")
for x, y in enumerate(data):
x0 = x * x_stretch + x * x_width + x_gap
y0 = c_height - (y * y_stretch + y_gap)
x1 = x * x_stretch + x * x_width + x_width + x_gap
y1 = c_height - y_gap
c.create_rectangle(x0, y0, x1, y1, fill="red")
c.create_text(x0 + 2, y0, anchor=tk.SW, text=str(y))
然后,只要数据更改,就可以调用此方法,以传入新数据。
但是,您的代码还有其他问题。您需要mainloop()
在程序的最后调用,因为它直到窗口被销毁后才返回。
您还需要使用after
来定期更新数据,或调用tkinter的update
函数以允许窗口重绘自身。
以下是编写代码底部以添加数据并每五秒钟重绘图形的方法:
def update():
count = 0
for i in range(5):
data.append(random.randint(1, 11))
c.delete("all")
draw_barchart(data)
root.after(5000, update)
update()
root.mainloop()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句