andy__h
Posts: 4
Joined: Wed Aug 12, 2020 8:35 am

Problem with tkinter & screen locking

Wed Aug 12, 2020 8:47 am

Hi,

I've recently been playing around with a 4B and an official touch screen, trying to make an alarm clock. I'm using python3 and tkinter to draw a full screen clock and its working well.

The only issue that I am having is that when the script is running in full screen, the screen lock activates after 10mins and presents a login prompt. Upon entering the password, the script has been terminated. Obviously this is no use as a clock.

I have both unclutter and xscreensaver installed - both of which work perfectly when displaying the desktop. It is only when I run the clock script that the screen lock activates.

Can anyone advise on what I can do to disable the screen lock?

Below is the code relating to tkinter. I have excluded the fuction 'show_time', which contains the clock specific code.

Code: Select all

#tkinter root
root = Tk()
root.attributes("-fullscreen", True)
root.configure(background='black')
root.bind("<Escape>", quit)
root.bind("x", quit)
root.after(1000, show_time)

txt.set(current_datetime.strftime("%H:%M"))
lbl = ttk.Label(root, textvariable=txt, font=fnt, foreground=font_colour, background="black")
lbl.place(relx=0.5, rely=0.5, anchor=CENTER)
fnt = font.Font(family='PibotoLT', size=225, weight='bold')
txt = StringVar()

root.mainloop()
Thanks,

Andy

User avatar
joelostinspace
Posts: 96
Joined: Sat Aug 10, 2019 2:51 pm
Location: Earth

Re: Problem with tkinter & screen locking

Thu Aug 13, 2020 12:38 am

andy,
root = Tk()
root.attributes("-fullscreen", True)
root.configure(background='black')
root.bind("<Escape>", quit)
root.bind("x", quit)
root.after(1000, show_time)
in your short snip it, the code is incomplete- I can't recreate your exact problem. But I can see some mistakes.
instead of using TK() as your background try making a Frame with root as master, background, width and height etc.. then your labels with the frame as master.
you are using the keyword quit as a function you can change it to quit_
you are using root.after to start your script just make a call to your function:

Code: Select all

from tkinter import Tk, Frame, Label,StringVar
from datetime import datetime

def quit_(event):
    root.destroy()
def show_time():
    txt.set(datetime.now())
    label.after(1000, show_time)
root= Tk()
frm= Frame(root,width=800,height=800,bg='skyblue')
frm.pack(expand='yes',fill='both')
root.bind('<Escape>', quit_)
txt= StringVar()
label= Label(frm,textvariable=txt,font=('arial',42,'bold'))
label.place(x=10,y=300)
show_time()
root.mainloop()

To travel is to discover that everyone is wrong about other countries.


--- Aldous Huxley

andy__h
Posts: 4
Joined: Wed Aug 12, 2020 8:35 am

Re: Problem with tkinter & screen locking

Thu Aug 13, 2020 7:01 pm

Thank you for taking a look at this. I have tried your suggestions, but ideally I want the clock to run fullscreen, rather than in a window/frame. I have attached all my code below for you to take a look at. Note that it calls a library to calculate sunrise and sunset.

Code: Select all

from datetime import datetime, time
from sunrise import sun
from tkinter import *
from tkinter import ttk
from tkinter import font

#Set lat long of location
location = sun(lat=0,long=0)

def quit_(event):
    root.destroy()

def show_time():
    
    #Define time and get sunrise and sunset
    current_datetime = datetime.now() #Required to pass to sun module & as main time variable
    current_time = datetime.time(current_datetime) #Required for comparison against output from sun module
    sunrise = location.sunrise(when=current_datetime)
    sunset = location.sunset(when=current_datetime)
    
    #Set font colour based on whether it is daylight
    if current_time>sunrise and current_time<sunset:
        font_colour="White"
    else:
        font_colour="Grey"
    
    txt.set(current_datetime.strftime("%H:%M:%S"))
    lbl = ttk.Label(root, textvariable=txt, font=fnt, foreground=font_colour, background="black")
    lbl.place(relx=0.5, rely=0.5, anchor=CENTER)
    
    root.after(1000, show_time)

root = Tk()
root.attributes("-fullscreen", True)
root.configure(background='black')
root.bind("<Escape>", quit)

fnt = font.Font(family='PibotoLT', size=225, weight='bold')
txt = StringVar()

show_time()

root.mainloop()

andy__h
Posts: 4
Joined: Wed Aug 12, 2020 8:35 am

Re: Problem with tkinter & screen locking

Thu Aug 13, 2020 9:25 pm

To give some more detail of the issue; when the clock is running in full screen, the screensaver kicks in at a seemingly random interval. This seems to be anywhere between 3 and 20mins. Once the screensaver has kicked in, the screen also locks. Upon entering the password and resuming the session, both the python script and any other running applications have been ended.

User avatar
joelostinspace
Posts: 96
Joined: Sat Aug 10, 2019 2:51 pm
Location: Earth

Re: Problem with tkinter & screen locking

Thu Aug 13, 2020 9:37 pm

The only thing I can see is in your function show_time you create a label and place it- 60 time a minute for ten minutes that's 600 times- I used # to remove modules and code related I don't have installed and moved

Code: Select all

font_colour="White"
lbl = ttk.Label(root, textvariable=txt, font=fnt, foreground=font_colour, background="black")
lbl.place(relx=0.5, rely=0.5, anchor=CENTER)
out of the function and under the StringVar and it ran fine
if you want to change the background color of a label use:

Code: Select all

 lbl.config(bg='gray')
in your show_time function
your bind method:
root.bind("<Escape>", quit)
is using the keyword quit not your function quit_

To travel is to discover that everyone is wrong about other countries.


--- Aldous Huxley

andy__h
Posts: 4
Joined: Wed Aug 12, 2020 8:35 am

Re: Problem with tkinter & screen locking

Fri Aug 14, 2020 6:25 pm

Thank you, your suggestions have resolved the issue.

lbl.config has also fixed my other issue, 'interference' on the screen during the endless redrawing of the text.

Very much appreciated :)

Return to “Python”