Reputation: 59
I'm working on simple window and like to have clock in it. Found out how to dynamically display time in separate window:
Somehow same code placed into my project displays only label's purple background:
def time():
string = strftime('%H:%M:%S %p')
lbl.config(text=string)
lbl.after(1000, time)
class MainWindow(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
main_frame = tk.Frame(self, height=600, width=1024)
main_frame.pack_propagate(0)
main_frame.pack(fill="both", expand="true")
main_frame.grid_rowconfigure(0, weight=1)
main_frame.grid_columnconfigure(0, weight=1)
frame1 = tk.LabelFrame(self, text="Parametry Pracy")
frame1.place(rely=0.05, relx=0.02, height=400, width=400)
pp=tk.Frame(frame1)
pp.pack(pady=2)
lbl = Label(pp, font=('arial', 20, 'bold'),bg='purple', fg='white', width=11)
lbl.grid(row=0, column=0, sticky="W", padx=20)
Upvotes: 0
Views: 52
Reputation: 59
Here is working code with changes:
class MainWindow(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
main_frame = tk.Frame(self, height=600, width=1024)
main_frame.pack_propagate(0)
main_frame.pack(fill="both", expand="true")
main_frame.grid_rowconfigure(0, weight=1)
main_frame.grid_columnconfigure(0, weight=1)
frame1 = tk.LabelFrame(self, text="Parametry Pracy")
frame1.place(rely=0.05, relx=0.02, height=400, width=400)
pp=tk.Frame(frame1)
pp.pack(pady=2)
def time():
string = strftime('%H:%M:%S %p')
lbl.config(text=string)
lbl.after(1000, time)
# Styling the label widget so that clock
# will look more attractive
lbl = Label(pp, font=('arial', 40, 'bold'), width=11)
lbl.grid(row=0, column=0, sticky="W", padx=20)
#lbl.pack(side = LEFT)
time()
Upvotes: 0