如何使Tkinter画布更新?

用户名

我已经在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()

我怎样才能解决这个问题?

布莱恩·奥克利(Bryan Oakley)

每当更改数字时,您将需要删除旧图并创建新图。

您应该通过移动代码以将条形图绘制为函数来执行此操作。

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Tkinter画布上更新情节?

来自分类Dev

如何在Tkinter画布上更新情节?

来自分类Dev

如何在Tkinter中更新,暂停和清除FigureCanvasTkAgg画布

来自分类Dev

如何在Tkinter画布上更新matplotlib子图?

来自分类Dev

如何为画布大小更新Tkinter滚动条大小

来自分类Dev

如何关闭 tkinter 画布?

来自分类Dev

如何更新 JavaFX 画布?

来自分类Dev

如何禁用Tkinter画布对象

来自分类Dev

更新Tkinter画布上的文本字段

来自分类Dev

如何更新画布上的图像?

来自分类Dev

如何防止文本在tkinter画布上重叠

来自分类Dev

Tkinter-如何水平居中画布文本?

来自分类Dev

Tkinter:如何为画布矩形的轮廓着色?

来自分类Dev

如何使Tkinter画布多边形透明?

来自分类Dev

Tkinter-如何隐藏画布上的标签?

来自分类Dev

如何在 tkinter 的画布中反转颜色?

来自分类Dev

tkinter 画布仅在出现错误时更新

来自分类Dev

如何更新tkinter标签

来自分类Dev

如何更新Tkinter标签?

来自分类Dev

如何更新在画布上书写的文本

来自分类Dev

画布内的 Python Tkinter 画布

来自分类Dev

如何使用tkinter画布小部件制作按钮?

来自分类Dev

Tkinter:如何使用箭头键滚动整个画布?

来自分类Dev

如何在画布Python Tkinter中将图像居中

来自分类Dev

如何在python-Tkinter的画布文本上放置轮廓

来自分类Dev

如何在tkinter画布中为封闭的折线着色?

来自分类Dev

如何在tkinter中停止重叠的画布图像

来自分类Dev

如何通过新窗口更改Tkinter画布的尺寸?

来自分类Dev

如何在Tkinter中的画布内移动文本