在不关闭窗口的情况下刷新tkinter中的热图

时间:2018-09-03 10:07:27

标签: python tkinter

我在刷新tkinter中的帧时遇到一些问题。我应该关闭窗口以刷新框架。我的热传感器值每次都在变化。我看了root.after(),但没有明白。

不关闭该怎么办?

我的代码在这里:

try:
    from tkinter import *
except ModuleNotFoundError:
    from tkinter import *  # Python 3




f = open('text.txt', 'r')
nodes_list = [[int(item) for item in line.split()] for line in f]
heat_map = nodes_list
heat_min = min(min(row) for row in heat_map)
heat_max = max(max(row) for row in heat_map)
print('hello')
palette = (0, 0, 1), (0, .5, 0), (0, 1, 0), (1, .5, 0), (1, 0, 0)


def pseudocolor(value, minval, maxval, palette):
    """ Maps given value to a linearly interpolated palette color. """
    max_index = len(palette)-1
    # Convert value in range minval...maxval to the range 0..max_index.
    v = (float(value-minval) / (maxval-minval)) * max_index
    i = int(v); f = v-i  # Split into integer and fractional portions.
    c0r, c0g, c0b = palette[i]
    c1r, c1g, c1b = palette[min(i+1, max_index)]
    dr, dg, db = c1r-c0r, c1g-c0g, c1b-c0b
    return c0r+(f*dr), c0g+(f*dg), c0b+(f*db)  # Linear interpolation.

def colorize(value, minval, maxval, palette):
        """ Convert value to heatmap color and convert it to tkinter color. """
        color = (int(c*255) for c in pseudocolor(value, minval, maxval, palette))
        return '#{:02x}{:02x}{:02x}'.format(*color)  # Convert to hex string.


root = Tk()
root.title('Heatmap')

width, height = 500, 500  # Canvas size.
rows, cols = len(heat_map), len(heat_map[0])
rect_width, rect_height = width // rows, height // cols
border = 1  # Pixel width of border around each.

canvas = Canvas(root, width=width, height=height)
canvas.pack()#goruntuyu gosteren kisim

for y, row in enumerate(heat_map):
    for x, temp in enumerate(row):
        x0, y0 = x * rect_width, y * rect_height
        x1, y1 = x0 + rect_width - border, y0 + rect_height - border
        color = colorize(temp, heat_min, heat_max, palette)
        canvas.create_rectangle(x0, y0, x1, y1, fill=color, width=0)


root.mainloop()

text.txt就是这样。

22 5 9 15 22 20 20 20
22 33 32 15 22 22 20 20
23 34 33 15 22 22 20 20
25 35 35 15 22 25 20 20
12 15 35 15 10 5 20 20
15 40 33 15 3 5 20 15
16 32 25 15 12 22 50 15
13 32 31 15 5 22 23 13

2 个答案:

答案 0 :(得分:1)

您想使用root.after重复调用一个函数来更改每个矩形的颜色。

我每秒都会随机选择一种颜色,以演示如何为您工作;您可能需要将其替换为定期从热传感器收集的更新数据。

import random

try:
    from tkinter import *
except ModuleNotFoundError:
    from tkinter import *  # Python 3


with open('text.txt', 'r') as f:   # <-- this construct automatically closes the file
    nodes_list = [[int(item) for item in line.split()] for line in f]

heat_map = nodes_list
heat_min = min(min(row) for row in heat_map)
heat_max = max(max(row) for row in heat_map)
print('hello')
palette = (0, 0, 1), (0, .5, 0), (0, 1, 0), (1, .5, 0), (1, 0, 0)


def pseudocolor(value, minval, maxval, palette):
    """ Maps given value to a linearly interpolated palette color. """
    max_index = len(palette)-1
    v = (float(value-minval) / (maxval-minval)) * max_index
    i = int(v); f = v-i
    c0r, c0g, c0b = palette[i]
    c1r, c1g, c1b = palette[min(i+1, max_index)]
    dr, dg, db = c1r-c0r, c1g-c0g, c1b-c0b
    return c0r+(f*dr), c0g+(f*dg), c0b+(f*db)

def colorize(value, minval, maxval, palette):
        """ Convert value to heatmap color and convert it to tkinter color. """
        color = (int(c*255) for c in pseudocolor(value, minval, maxval, palette))
        return '#{:02x}{:02x}{:02x}'.format(*color)


def update_colors():
    for rect in rectangles:
        color = random.choice(colors)            # <--- replace with heat sensor data feed
        canvas.itemconfig(rect, fill=color)
    root.after(1000, update_colors)

root = Tk()
root.title('Heatmap')

width, height = 500, 500  # Canvas size.
rows, cols = len(heat_map), len(heat_map[0])
rect_width, rect_height = width // rows, height // cols
border = 1  # Pixel width of border around each.

canvas = Canvas(root, width=width, height=height)
canvas.pack()#goruntuyu gosteren kisim

rectangles = []    # <-- keep a handle on each of the canvas rectangles
colors = []        # <-- this to have a list of colors to pick from to demonstrate

for y, row in enumerate(heat_map):
    for x, temp in enumerate(row):
        x0, y0 = x * rect_width, y * rect_height
        x1, y1 = x0 + rect_width - border, y0 + rect_height - border
        color = colorize(temp, heat_min, heat_max, palette)
        colors.append(color)
        rectangles.append(canvas.create_rectangle(x0, y0, x1, y1, fill=color, width=0))

update_colors()

root.mainloop()

[编辑]:

如果传感器值位于文本文件中,则需要获取它们。这可以通过提取函数中的文件开头来完成。

伪代码

def get_sensor_values():
    with open('text.txt', 'r') as f:
        return [[int(item) for item in line.split()] for line in f]  # <-- the nodes list

然后在update_colors()方法中:

def update_colors():
    nodes_list = get_sensor_values()
    for node, rect in zip(nodes_list, rectangles):  # <--- make sure the nodes and the rectangles correspond to the same index in their respective lists
        fill = calculate the color value from the node value   # <-- the same way you already assign an initial color to each rectangle
        canvas.itemconfig(rect, fill=color)
    root.after(1000, update_colors)

答案 1 :(得分:0)

在这里尝试一下。

将此功能添加到您的代码中。

def repeat():
    global f 
    f = open('text.txt', 'r')
    # This function will be called every 100millisecs 
    root.after(100, repeat) 

像这样在root.mainloop()之前调用它。

root.after(100, repeat)
root.mainloop()