简体   繁体   English

如何每4秒更新一次tkinter窗口?

[英]How to update tkinter window every 4 seconds?

I am trying to create a program that reads lines from a file and puts them into a tkinter window. 我正在尝试创建一个程序,该程序从文件中读取行并将其放入tkinter窗口。 At the moment my code is this: 目前,我的代码是这样的:

def read_notifications():
    #def update():
    #   window.config(text=str(random.random()))
    #   window.after(1000, update)
    aaa = 1
    while True:
        re = open("Y:/System Info/notifications.txt", "r")
        rf = re.read()
        rh = rf.count("\n")
        re.close()
        lines = [line.rstrip('\n') for line in open("Y:/System Info/notifications.txt")]
        rk = -1
        while True:
            aaa = aaa + 2
            rk = rk + 1
            #print(lines[rk])
            rl = rk + 1
            ya = lines[rk].split("#")
            yb = str(tomrt)
            if ya[1] == yb:
                yc = "Tommorow"
            else:
                if ya[1] == "0":
                    yc = "Monday"
                if ya[1] == "1":
                    yc = "Tuesday"
                if ya[1] == "2":
                    yc = "Wednesday"
                if ya[1] == "3":
                    yc = "Thursday"
                if ya[1] == "4":
                    yc = "Friday"
                if ya[1] == "5":
                    yc = "Saturday"
                if ya[1] == "6":
                    yc = "Sunday"       
            c = 650
            window = tk.Tk()
            #back = tk.Frame(width=700, height=c)
            #back.pack()
            window.title("Notifications")
            window.iconbitmap("1235.ico")
            #Subject
            lbl = tk.Label(window, text=ya[0])
            lbl.config(font=("Courier", 18))
            lbl.grid(column=0, row=0)
            #lbl.pack(side=tk.LEFT,)
            #Day
            lbl = tk.Label(window, text=" " + yc)
            lbl.config(font=("Courier", 16))
            lbl.grid(column=2, row=0)
            #Type
            lbl = tk.Label(window, text=ya[2])
            lbl.config(font=("Courier", 16))
            lbl.grid(column=4, row=0)
            #Descripion
            lbl = tk.Label(window, text=ya[4])
            lbl.config(font=("Courier", 16))
            lbl.grid(column=6, row=0)
            #lbl.pack(side=tk.LEFT)
            #window.after(1000, update)
            if rl == rh:
                print("hello")
                break
        if aaa > 2:
            time.sleep(4)
            window.destroy()
        else:
            window.mainloop()

I am not sure why this doesn't work properly. 我不确定为什么这不能正常工作。 I am trying to make it so the tkinter window will update itself every 4 seconds, as another program is making changes to the notifications.txt file and I want the tkinter windows to update accordingly. 我正在尝试这样做,以便tkinter窗口每4秒更新一次,因为另一个程序正在对notifications.txt文件进行更改,并且我希望tkinter窗口相应地更新。

You code is a bit hard to re-work but here is a working example of how one can monitor a file. 您的代码很难重新编写,但这是一个如何监视文件的有效示例。

Say I have a file that contains 4 lines and we call this file my_file : 假设我有一个包含4行的文件,我们将此文件称为my_file

1 test
2 testing
3 more testing
4 test again

All we want to do is update the labels ever 4 seconds so we can use the after() method to keep us going. 我们要做的就是每4秒钟更新一次标签,以便我们可以使用after()方法使我们继续前进。

Take a look at this below code. 看看下面的代码。

import tkinter as tk


window = tk.Tk()
window.title("Notifications")
file_to_monitor = "./my_file.txt"

def read_notifications():
    with open(file_to_monitor, "r") as f:
        x = f.read()
        string_list = x.split("\n")

    lbl1.config(text=string_list[0])
    lbl2.config(text=string_list[1])
    lbl3.config(text=string_list[2])
    lbl4.config(text=string_list[3])

    window.after(4000, read_notifications)


lbl1 = tk.Label(window, text="")
lbl1.grid(column=0, row=0, stick="w")
lbl2 = tk.Label(window, text="")
lbl2.grid(column=0, row=1, stick="w")
lbl3 = tk.Label(window, text="")
lbl3.grid(column=0, row=2, stick="w")
lbl4 = tk.Label(window, text="")
lbl4.grid(column=0, row=3, stick="w")

read_notifications()
window.mainloop()

What we start out with on the first read: 我们首先读到的内容是:

在此处输入图片说明

Then after I have made a change to the file and saved from my notepad: 然后,我对文件进行了更改并从记事本中保存后:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM