Sahil Mohile
Sahil Mohile

Reputation: 99

Exiting out of FullScreen after 2 minutes

I am trying to open fullscreen via tkinter and closing windows after 2 mins but fullscreen doesn't exit.

root = tk.Tk()


def fullscreen ():
    text1 = tk.Text(root)
    text1.tag_config("center", justify='center')
    text1.insert(1.0, "Its your Break Time which will continue for two minutes. Take a walk and rest your eyes")
    # text1.tag_add("center", "1.0", "end")
    text1.pack()

    root.attributes("-fullscreen", True)

    root.bind("<F11>", lambda event: root.attributes("-fullscreen",
                                        not root.attributes("-fullscreen")))
    root.bind("<Escape>", lambda event: root.attributes("-fullscreen", False))
    root.mainloop()
    # time.sleep(10)

    # root.attributes("-fullscreen", False)
    # root.mainloop()

def exitFullscreen (): 
    time.sleep(10)
    print("Its exceuting")
    root.attributes("-fullscreen", False)
    root.mainloop()


total_break = 1
break_count = 0


while (break_count < total_break):
    time.sleep(1800)
    speak("Its time for you to take break")
    fullscreen()
    start_thread = threading.Thread(target = fullscreen())
    end_thread = threading.Thread(target= exitFullscreen())

    start_thread.start()
    end_thread.start()

    # root.mainloop()
    break_count+=1

I tried using threading methods to run both functions at same time.

I have also tried using time.sleep to exit screen after 2 mins

while (break_count < total_break):
    time.sleep(1800)
    speak("Its time for you to take break")
    fullscreen()
    time.sleep(180)
    print("Its exceuting")
    root.attributes("-fullscreen", False)
    root.mainloop()

    break_count+=1

I have also tried using sys.exit(fullscreen) in while loop but fullscreen function doesn't exit after 2 mins to execute next lines of code.

Upvotes: 0

Views: 51

Answers (1)

acw1668
acw1668

Reputation: 46678

You can simply use .after() instead of using threads:

import tkinter as tk
from pyttsx3 import speak

root = tk.Tk()
root.withdraw()
root.config(bg="black")

message = """\
Its your Break Time which will continue for two minutes.
Take a walk and rest your eyes"""

tk.Label(root, text=message, font="Arial 48 bold", fg="green", bg="black").place(relx=0.5, rely=0.5, anchor="c")

#root.attributes("-fullscreen", True)
#root.bind("<F11>", lambda event: root.attributes("-fullscreen", not root.attributes("-fullscreen")))
#root.bind("<Escape>", lambda event: root.attributes("-fullscreen", False))

def exitFullscreen(break_count):
    print("Its executing")
    root.withdraw() # hide root window
    if break_count < total_break:
        # schedule next break
        root.after(work_period, fullscreen, break_count+1)

def fullscreen(break_count):
    # show root window
    root.attributes("-fullscreen", True)
    root.deiconify()
    # schedule to hide root window
    root.after(break_period, exitFullscreen, break_count)
    root.update()
    speak("Its time for you to take break")

work_period = 1000 * 1800  # 30 minutes
break_period = 1000 * 120  # 2 minutes
total_break = 2

# schedule first break
root.after(work_period, fullscreen, 1)
root.mainloop()

Upvotes: 2

Related Questions