如何在几秒钟内更新一个tkinter标签

huangapple go评论59阅读模式
英文:

How to update a tkinter label every few seconds

问题

import tkinter as tk
import datetime
import pandas as pd

global startzeit
def timer():
    global currenttime
    currenttime = datetime.datetime.today()
    window.after(1000, timer)

def log():
    global startzeit
    startzeit = datetime.datetime.today()
    List = [(startzeit)]
    print (List)
    label1.config(text="Error has been logged")
    
    
window = tk.Tk()
window.title("DPT10")
window.geometry("210x80")

label1 = tk.Label(text=currenttime, padx=10, pady=5)
label1.place(x=10, y=5, width=200, height=20)

button = tk.Button(window, text="Log Error", command=log)
button.place(x=70, y=45, width=70, height=20)

def update_label(label1):
    new_text = currenttime
    label1.configure(text=new_text)
    label1.after(1000, update_label, label1)
update_label(label1)
timer()

window.mainloop()
df = pd.DataFrame({'Errors' : List})
英文:

I am writing a code that I basically just want to log the time and date into a pandas dataframe. Basically I want it to show the current time, and if you press the "log error" button I want it to show that the error was logged. I would like the code to update every few seconds, or even every minute. I would like this program to remain in the background, so updating the time every so often would be necessary. I would also like it to return to the current date and time after the log error was pressed. I am wondering if there is a way I could solve both of my issues and have the window itself update every so often. Below is my current code.

import tkinter as tk
import threading
import datetime
import pandas as pd

global startzeit

startzeit = datetime.datetime.today()


def log():
    global startzeit
    startzeit = datetime.datetime.today()
    List = [(startzeit)]
    print (List)
    label1.config(text="Error has been logged")
    label1



window = tk.Tk()
window.title("DPT10")
window.geometry("210x80")


label1 = tk.Label(text=startzeit, padx=10, pady=5)
label1.place(x=10, y=5, width=200, height=20)


button = tk.Button(window, text="Log Error", command=log)
button.place(x=70, y=45, width=70, height=20)


window.mainloop()


df = pd.DataFrame({Errors : List})

I had tried adding Threading, and used a threading timer to try and get a variable label. I am not quite sure how to implement it correctly though.

Update on this, I was able to get it to do what I want by adding both of the first two suggestions. My code below gets it to update every second and log the time and date whenever the button is pressed. Still working to improve it. I want to have it pause for about 5 seconds on the Error. will update if I am able.

import tkinter as tk
import datetime
import pandas as pd

global startzeit
def timer():
    global currenttime
    currenttime = datetime.datetime.today()
    window.after(1000, timer)

def log():
    global startzeit
    startzeit = datetime.datetime.today()
    List = [(startzeit)]
    print (List)
    label1.config(text="Error has been logged")
    
    
window = tk.Tk()
window.title("DPT10")
window.geometry("210x80")

label1 = tk.Label(text=currenttime, padx=10, pady=5)
label1.place(x=10, y=5, width=200, height=20)

button = tk.Button(window, text="Log Error", command=log)
button.place(x=70, y=45, width=70, height=20)

def update_label(label1):
    new_text = currenttime
    label1.configure(text=new_text)
    label1.after(1000, update_label, label1)
update_label(label1)
timer()

window.mainloop()
df = pd.DataFrame({'Errors' : List})

答案1

得分: 1

创建一个函数,用于更新标签,并使用 `after` 方法安排它自己再次运行。

def update_label(label):
new_text = <你的代码在这里>
label.configure(text=new_text)
label.after(1000, update_label, label)


一旦你调用这个函数,它将会每秒左右被调用一次。
英文:

Create a function that updates the label, have it schedule itself to be run again using after.

def update_label(label):
    new_text = &lt;your code here&gt;
    label.configure(text=new_text)
    label.after(1000, update_label, label)

Once you call this function once, it will continue to be called approximately once per second.

答案2

得分: 0

你可以使用内置的 tkinter.after() 方法,在给定的毫秒数后调用一个函数。你也可以使用 after() 让函数自己调用,这样它可以在固定的时间间隔内重复调用。

# 这里实际上不需要 'global startzeit',只在 'log' 函数内部需要
startzeit = datetime.datetime.today()

def log():
    global startzeit
    startzeit = datetime.datetime.today()
    List = [(startzeit)]
    print (List)
    label1.config(text="错误已记录")
    window.after(1000, log)  # 每隔1000毫秒再次调用这个函数

为了开始更新,你需要在调用 mainloop() 之前调用一次 log()

英文:

You can use the built-in tkinter.after() method to call a function after a given number of milliseconds. You can also have a function call itself with after() so it can be called repeatedly at a regular interval

# you don&#39;t actually need &#39;global startzeit&#39; here, only inside &#39;log&#39;
startzeit = datetime.datetime.today()


def log():
    global startzeit
    startzeit = datetime.datetime.today()
    List = [(startzeit)]
    print (List)
    label1.config(text=&quot;Error has been logged&quot;)
    window.after(1000, log)  # call this function again every 1000mS

In order to start updating, you'll need to call log() once before the call to mainloop()

huangapple
  • 本文由 发表于 2023年2月14日 03:32:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定