Reputation: 23
I am trying to a create a GUI that will display the current local time and give you a time output when you press the 'Lap' button. So far, I have create the clock and the buttons but I can't seem to figure out how to create an output box in the window that will display the timestamp when the button is pressed. I have very little programming experience so any help would be appreciated!
I have attached what I have so far:
import tkinter
import time
class Clock(tkinter.Label):
def __init__(self, parent=None, seconds=True, colon=False):
tkinter.Label.__init__(self, parent)
self.display_seconds = seconds
if self.display_seconds:
self.time = time.strftime('%I:%M:%S')
else:
self.time = time.strftime('%I:%M %p').lstrip('0')
self.display_time = self.time
self.configure(text=self.display_time)
if colon:
self.blink_colon()
self.after(200, self.tick)
def tick(self):
if self.display_seconds:
new_time = time.strftime('%I:%M:%S')
else:
new_time = time.strftime('%I:%M %p').lstrip('0')
if new_time != self.time:
self.time = new_time
self.display_time = self.time
self.config(text=self.display_time)
self.after(200, self.tick)
def timestamp():
print(time.strftime("%I:%M:%S"))
if __name__ == "__main__":
window = tkinter.Tk()
frame = tkinter.Frame(window, width=800, height=800)
frame.pack()
tkinter.Label(frame, text="Current time: ").pack()
clock1 = Clock(frame)
clock1.pack()
clock1.configure(bg='white', fg='black', font=("helvetica", 65))
tkinter.Label(frame, text=" ").pack()
b = tkinter.Button(frame, text='Quit', command=quit)
b.pack(side=tkinter.RIGHT)
b2 = tkinter.Button(frame, text='Lap', command=timestamp)
b2.pack(side=tkinter.LEFT)
window.mainloop()
I need help to create an output box in the window that will print the time when the 'Lap' button is pressed.
Upvotes: 2
Views: 553
Reputation: 4407
You can create a ScrolledText
and insert into it using the insert("end", value)
command whenever the button is pressed. Here is the augmented code.
import tkinter
import time
from tkinter import scrolledtext
class Clock(tkinter.Label):
def __init__(self, parent=None, seconds=True, colon=False):
tkinter.Label.__init__(self, parent)
self.display_seconds = seconds
if self.display_seconds:
self.time = time.strftime('%I:%M:%S')
else:
self.time = time.strftime('%I:%M %p').lstrip('0')
self.display_time = self.time
self.configure(text=self.display_time)
if colon:
self.blink_colon()
self.after(200, self.tick)
def tick(self):
if self.display_seconds:
new_time = time.strftime('%I:%M:%S')
else:
new_time = time.strftime('%I:%M %p').lstrip('0')
if new_time != self.time:
self.time = new_time
self.display_time = self.time
self.config(text=self.display_time)
self.after(200, self.tick)
def timestamp():
print(time.strftime("%I:%M:%S"))
if __name__ == "__main__":
window = tkinter.Tk()
frame = tkinter.Frame(window, width=800, height=800)
frame.pack()
tkinter.Label(frame, text="Current time: ").pack()
text = scrolledtext.ScrolledText(frame, height=10) ##
text.pack() ##
clock1 = Clock(frame)
clock1.pack()
clock1.configure(bg='white', fg='black', font=("helvetica", 65))
tkinter.Label(frame, text=" ").pack()
b = tkinter.Button(frame, text='Quit', command=quit)
b.pack(side=tkinter.RIGHT)
b2 = tkinter.Button(frame, text='Lap', command=lambda :text.insert("end", time.strftime("%I:%M:%S")+'\n')) ##
b2.pack(side=tkinter.LEFT)
window.mainloop()
Upvotes: 1