Reputation: 11
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})
Upvotes: 0
Views: 1296
Reputation: 6800
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't actually need 'global startzeit' here, only inside 'log'
startzeit = datetime.datetime.today()
def log():
global startzeit
startzeit = datetime.datetime.today()
List = [(startzeit)]
print (List)
label1.config(text="Error has been logged")
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()
Upvotes: 0
Reputation: 386020
Create a function that updates the label, have it schedule itself to be run again using after
.
def update_label(label):
new_text = <your code here>
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.
Upvotes: 1