Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

python - Making a "screen locker" with tkinter and schedule module

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...