0

I'm working on simple window and like to have clock in it. Found out how to dynamically display time in separate window: enter image description here

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)

enter image description here

3
  • 1
    You do not appear to be calling the time() function. Even if you did, it has no access to lbl, which is a local variable that does not exist outside of MainWindow.__init__(). Commented Jul 24, 2024 at 21:14
  • Moved def Time() under main window class + added time() and works! Thanks Mate! Commented Jul 24, 2024 at 21:19
  • 1
    Ignore time while using valid Python. Go by a different name. Commented Jul 25, 2024 at 12:36

1 Answer 1

0

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()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.