我在刷新框架时遇到一些问题tkinter。我应该关闭窗口来刷新我的框架。我的热传感器值每次都在变化。我看了root.after(),但我没有得到它。不关闭怎么办?我的代码在这里:try: from tkinter import *except ModuleNotFoundError: from tkinter import * # Python 3f = open('text.txt', 'r')nodes_list = [[int(item) for item in line.split()] for line in f]heat_map = nodes_listheat_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 // colsborder = 1 # Pixel width of border around each.canvas = Canvas(root, width=width, height=height)canvas.pack()#goruntuyu gosteren kisimtext.txt 是这样的。22 5 9 15 22 20 20 2022 33 32 15 22 22 20 2023 34 33 15 22 22 20 2025 35 35 15 22 25 20 2012 15 35 15 10 5 20 2015 40 33 15 3 5 20 1516 32 25 15 12 22 50 1513 32 31 15 5 22 23 13
2 回答
神不在的星期二
TA贡献1963条经验 获得超6个赞
在这里试试这个。
将此函数添加到您的代码中。
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()
添加回答
举报
0/150
提交
取消