在Tkinter中更新文本小部件中的值

时间:2020-07-24 23:50:12

标签: python tkinter

我需要在10秒后更新同一Tkinter窗口中其他一些数据的值,而不关闭该窗口。我已经尝试使用Tkinter的MatTable方法,但是它不适用于我的目的。 <span contentEditable=true></span>是一个三列多行的数组,需要在此窗口中显示。

after

1 个答案:

答案 0 :(得分:0)

您可以首先使用空的窗口小部件Text创建网格并将其保留在2D列表中。

然后,您可以运行从小部件Text中删除旧数据并放入新数据的函数。最后,它应该使用after()在10秒后再次运行。

问题可以使代码在depth_chart中获取新数据,但是您没有显示此部分,因此我模拟了depth_chart中的变化


在此示例中,我创建随机数据并每1秒更新一次。

import tkinter as tk # PEP8: `import *` is not preferred
import random  # simulate changes in data

# --- functions ---

def fill_text():
    global depth_chart

    for y, row in enumerate(depth_chart):
        for x, item in enumerate(row):
            # remove previous text
            all_text[y][x].delete('0.1', 'end')
            # add new text
            if item is not None:
                all_text[y][x].insert('insert', item)

    # simulate changes in data                
    depth_chart = [[random.randint(0, 10) for x in range(3)] for y in range(10)]

    # fill it again after 1s (1000ms)
    root.after(1000, fill_text)
    
# --- main ---

# simulate changes in data
depth_chart = [[random.randint(0, 10) for x in range(3)] for y in range(10)]

root = tk.Tk()
root.title("Depth Ladder")

tk.Label(root, text="Bid Quantity").grid(row=0, column=0)  # PEP8: without spaces around `=`
tk.Label(root, text="Price").grid(row=0, column=1)
tk.Label(root, text="Ask Quantity").grid(row=0, column=2)

# create grid with empty widgets `Text` and remember them on 2D list
all_text = []
for y, row in enumerate(depth_chart, 1): 
    row_text = []
    for x, item in enumerate(row):
        text = tk.Text(root, height=1)
        text.grid(row=y, column=x)
        
        if x == 0:
            text.config(bg="green", fg="white", relief='ridge', width=8)
        elif x == 1:
            text.config(bg="black", fg="white", relief='ridge', width=12)
        elif x == 2:
            text.config(bg="red", fg="white", relief='ridge', width=8)

        row_text.append(text)

    all_text.append(row_text)
        
# fill it first time        
fill_text()

root.mainloop()

编辑:

如果每10秒钟depth_chart中的行数不同,则从网格中删除所有Text并再次创建新的小部件会更容易-然后您可以在{{1 }}已创建。

它有一个问题-删除所有小部件后,它会闪烁

Text

要解决闪烁问题,将需要更复杂的代码来删除或仅添加所需的行。

import tkinter as tk # PEP8: `import *` is not preferred
import random  # simulate changes in data

# --- functions ---

def fill_text():
    global depth_chart
    global all_text

    # simulate changes in data                
    number_of_rows = random.randint(5, 20)
    depth_chart = [[random.randint(0, 10) for x in range(3)] for y in range(number_of_rows)]
    
    # delete old text widgets
    for row in all_text: 
        for item in row:
            item.destroy()
            
    # clear list
    all_text = []
    
    # create grid with widgets `Text` and remember them on 2D list
    for y, row in enumerate(depth_chart, 1): 
        row_text = []
        for x, item in enumerate(row):
            text = tk.Text(root, height=1)
            text.grid(row=y, column=x)
            
            if x == 0:
                text.config(bg="green", fg="white", relief='ridge', width=8)
            elif x == 1:
                text.config(bg="black", fg="white", relief='ridge', width=12)
            elif x == 2:
                text.config(bg="red", fg="white", relief='ridge', width=8)

            if item is not None:
                text.insert('insert', item)
    
            row_text.append(text)
    
        all_text.append(row_text)

    # fill it again after 1s (1000ms)
    root.after(1000, fill_text)
    
# --- main ---

# place for all text widgets
all_text = []

root = tk.Tk()
root.title("Depth Ladder")

tk.Label(root, text="Bid Quantity").grid(row=0, column=0)  # PEP8: without spaces around `=`
tk.Label(root, text="Price").grid(row=0, column=1)
tk.Label(root, text="Ask Quantity").grid(row=0, column=2)
        
# fill it first time        
fill_text()

root.mainloop()
相关问题