I've been wanting to create a program to "lock my screen" (don't ask why) from 11 PM to 6 AM. I recently found schedule, which allows me to run a function at a specific time. I tried to integrate it with tkinter, but something unexpected happened. Here is my code so far:
from tkinter import *
from tkinter import messagebox, simpledialog
import schedule
import time
root = Tk()
def block_start():
def provide_password_to_quit():
user_result = simpledialog.askstring('Password', 'What is the password?')
if user_result.lower() == 'password':
root.destroy()
global root
root.geometry('1920x1080')
root.overrideredirect(True) # makes root full-screen, which "locks" my monitor
quit_button = Button(root, text="Quit", command=provide_password_to_quit)
quit_button.pack()
root.mainloop()
def block_off():
global root
root.overrideredirect(False) # "unlocks" my monitor
schedule.every().day.at("23:00").do(block_start) # creates full-screened window at 11 PM
schedule.every().day.at("06:00").do(block_off) # destroys full-screened window at 6 AM
while True:
schedule.run_pending()
time.sleep(1)
When I run this, it creates the full-screened window, but when trying to unlock my monitor at 6:00, it doesn't do anything, and once I close the full-screen, it then decides to run and raise _tkinter.TclError
(because I destroyed root). Is there any way to work around this? I tried putting it in threads, but tkinter doesn't allow you to run GUIs not in the main thread.
question from:
https://stackoverflow.com/questions/65947858/making-a-screen-locker-with-tkinter-and-schedule-module 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…