Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在tkinter中刷新热图而不关闭窗口_Python_Tkinter - Fatal编程技术网

Python 在tkinter中刷新热图而不关闭窗口

Python 在tkinter中刷新热图而不关闭窗口,python,tkinter,Python,Tkinter,我在刷新tkinter中的帧时遇到一些问题。我应该关闭窗口以刷新帧。我的热传感器值每次都在变化。我查看了root.after(),但没有找到它 不关闭它我怎么办? 我的代码在这里: 试试看: 从tkinter进口* 除ModuleNotFoundError外: 从tkinter导入*#Python 3 f=打开('text.txt','r') nodes_list=[[int(item)表示第行中的项。split()]表示第f行] 加热映射=节点列表 加热最小值=最小值(加热图中行的最小值(行)

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

不关闭它我怎么办?

我的代码在这里:

试试看:
从tkinter进口*
除ModuleNotFoundError外:
从tkinter导入*#Python 3
f=打开('text.txt','r')
nodes_list=[[int(item)表示第行中的项。split()]表示第f行]
加热映射=节点列表
加热最小值=最小值(加热图中行的最小值(行))
加热最大值=最大值(加热贴图中行的最大值(行))
打印('你好')
调色板=(0,0,1),(0,5,0),(0,1,0),(1,5,0),(1,0,0)
def伪彩色(值、最小值、最大值、调色板):
“”“将给定值映射到线性插值调色板颜色。”“”
最大索引=长度(调色板)-1
#将范围minval…maxval中的值转换为范围0..max\u索引。
v=(浮点(值最小值)/(最大值最小值))*max_索引
i=int(v);f=v-i#分为整数和小数部分。
c0r、c0g、c0b=调色板[i]
c1r,c1g,c1b=调色板[min(i+1,max_索引)]
dr,dg,db=c1r-c0r,c1g-c0g,c1b-c0b
返回c0r+(f*dr)、c0g+(f*dg)、c0b+(f*db)#线性插值。
def颜色化(值、最小值、最大值、调色板):
“”“将值转换为热图颜色并将其转换为tkinter颜色。”“”
颜色=(int(c*255)表示伪彩色中的c(值、最小值、最大值、调色板))
返回“#{:02x}{:02x}{:02x}”。格式(*颜色)#转换为十六进制字符串。
root=Tk()
root.title('Heatmap')
宽度、高度=500500#画布大小。
行,cols=len(热图),len(热图[0])
矩形宽度,矩形高度=宽度//行,高度//列
边框=1#每个边框周围的像素宽度。
画布=画布(根,宽度=宽度,高度=高度)
canvas.pack()35; goruntuyu gosteren kisim
对于y,枚举中的行(热图):
对于x,枚举中的临时值(行):
x0,y0=x*rect\u宽度,y*rect\u高度
x1,y1=x0+矩形宽度-边框,y0+矩形高度-边框
颜色=着色(温度、最低热量、最高热量、调色板)
画布。创建矩形(x0,y0,x1,y1,填充=颜色,宽度=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
root.after(100, repeat)
root.mainloop()
试试这个

将此函数添加到代码中

def repeat():
    global f 
    f = open('text.txt', 'r')
    # This function will be called every 100millisecs 
    root.after(100, repeat) 
然后像这样在
root.mainloop()之前调用它

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
root.after(100, repeat)
root.mainloop()

您想使用
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()

text.txt由您的热传感器更新?是的。它每100毫秒改变一次,我只举一个例子,说明txt包含的内容。我可以在我的txt中更改它。但是,框架不会更改。请重新运行
节点列表
以及更新变量所需的任何行。全局f=open('text.txt',r')^SyntaxError:预期的语句结尾语法无效。这表明我已经更新了答案,请检查。抱歉输入错误不,它不起作用。我想。我只读文本,但并没有在列表上写。它具有传感器值'rectangles=[]。#不,您只需要根据传感器的值更新颜色。使用open('text.txt',r')作为f:此部分将传感器值作为字符串读取。|nodes_list=[[int(item)表示第行中的项。split()]表示第f行中的项]。这部分将其转换为整数如果节点列表中有传感器值,如何更改此部分?颜色=随机。选择(颜色)。谢谢你的帮助有没有办法把值作为整数而不是颜色值?因为华氏温度将在15-50度之间。我的节点列表包含整数值。另外,填充=或颜色=哪一个?如果我给一个值填充,我的颜色没有变化。canvas.itemconfig(rect,fill=color)在这个部分是的,您可以在字典中用颜色字符串映射温度:例如
colors={10:''00ce00',11:''0015d3',等等。
,并像这样访问它们:
fill=colors[temperature]