Sunday, May 1, 2022

Create a Lock Screen in Windows with PyQt6

This demo program shows how to create a lockscreen and detect any movement within the Windows OS. 

The Output: 

 


The Code: 

 


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import sys
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
import time
from ctypes import *
x_idle = 0
x_idle_toggle = 0
start_tmr = 0
class XScreenSaverInfo(Structure):
    _fields_ = [('window', c_long),
                ('state', c_int),
                ('kind', c_int),
                ('til_or_since',c_ulong),
                ('idle', c_ulong),
                ('eventMask', c_ulong)]

class LASTINPUTINFO(Structure):
    _fields_ = [
        ('cbSize', c_uint),
        ('dwTime', c_uint),
    ]
def get_idle_duration():
    lastInputInfo = LASTINPUTINFO()
    lastInputInfo.cbSize = sizeof(lastInputInfo)
    windll.user32.GetLastInputInfo(byref(lastInputInfo))
    millis = windll.kernel32.GetTickCount() - lastInputInfo.dwTime
    return millis / 1000.0

class inputdialogdemo(QMainWindow):
    def __init__(self, parent = None):
      super(inputdialogdemo, self).__init__(parent)
      fnt = QFont('Open Sans', 16, QFont.Weight.Bold)
      self.lblIdle = QLabel('Idle Time', self)
      self.lblIdle.setFont(fnt)
      self.lblIdle.setGeometry(20, 40, 300, 30)
      pb3 = QPushButton('Show Lockscreen ', self)
      pb3.setGeometry(50, 100, 130, 30)
      
      pb3.clicked.connect(self.onClick_pb3)
      pb5 = QPushButton('Reset Idle', self)
      pb5.setGeometry(200, 100, 130, 30)
      
      pb5.clicked.connect(self.onClick_pb5)
      self.timer = QTimer(self)
      self.timer.timeout.connect(self.showTime)
      self.timer.start(1000) # update every second
      
      self.move(100, 200)
      self.setFixedSize(400, 300)
      self.setWindowTitle('Post 30')
    def exec_idel(self):
      global x_idle_toggle
      global x_idle
      global start_tmr
        
      while x_idle_toggle ==1:
        GetLastInputInfo = int(get_idle_duration())
        print(GetLastInputInfo)
        if GetLastInputInfo == 1:
          start_tmr = 1
          x_idle = 0
          x_idle_toggle = 0
          #self.timer.stop()
          self.buildExamplePopup()
          break
        if GetLastInputInfo == 1:
          start_tmr = 1
          x_idle = 0
          x_idle_toggle = 0
          #self.timer.stop()
          self.buildExamplePopup()
          break
        time.sleep(1)      
    def start_timer(self):
        global x_idle
        x_idle = 0 
        #self.timer = QTimer(self)
        #self.timer.timeout.connect(self.showTime)
        self.lblIdle.setText("Idle Time: " + str(x_idle))
        self.timer.start(1000) # update every second
        
        
    def mousePressEvent(self, QMouseEvent):
        global x_idle
        x_idle = 0     
    def showTime(self):
        global x_idle
        global x_idle_toggle
        global start_tmr
        self.lblIdle.setText("Idle Time: " + str(x_idle))
        if start_tmr == 0:
           x_idle = x_idle + 1
        if x_idle > 10 and x_idle_toggle == 0:
           x_idle_toggle = 1
           x_idle = 0
           self.exec_idel()
    def onClick_pb5(self):
        global x_idle
        x_idle = 0 
        self.lblIdle.setText("Idle Time: " + str(x_idle))
        self.timer.start(1000)
    def onClick_pb3(self):
        global x_idle
        global start_tmr
        start_tmr = 1
        x_idle = 0
        #self.timer.stop()
        self.buildExamplePopup()
    def buildExamplePopup(self):
        name = 'Lock Screen'
        self.exPopup = examplePopup(name)
        
        #self.exPopup.setGeometry(self.pos().x()+50, self.pos().y()+75, 300, 225)
        self.exPopup.move(self.pos().x()+50, self.pos().y()+75)
        self.exPopup.setFixedSize(300, 225)
        self.exPopup.show()
        
class examplePopup(QMainWindow):
    def __init__(self, name):
        super().__init__()
        
        self.name = name
        self.setWindowFlags(Qt.WindowType.Window | Qt.WindowType.CustomizeWindowHint | Qt.WindowType.WindowStaysOnTopHint)
        self.initUI()

    def initUI(self):
        fnt = QFont('Open Sans', 16, QFont.Weight.Bold)
        lblName = QLabel(self.name, self)
        lblName.setFont(fnt)
        lblName.setGeometry(50, 40, 200, 30)
        lblpwd =  QLabel('Password', self)
        lblpwd.setStyleSheet('QLabel {background-color: transparent; color: black;}')
        lblpwd.setGeometry(25, 115, 100, 30) 
        txtpwd = QLineEdit('', self) 

        txtpwd.setPlaceholderText("Enter Password Here")
        txtpwd.setGeometry(110, 115, 150, 30) 
        txtpwd.setEchoMode(QLineEdit.EchoMode.Password)
        
        pb4 = QPushButton('Ok', self)
        pb4.move(90, 175)
        
        pb4.clicked.connect(self.onClick_pb4)
        
    def mousePressEvent(self, QMouseEvent):
        global x_idle
        x_idle = 0 
    def onClick_pb4(self):
        global x_idle
        global start_tmr 

        start_tmr = 0
        
        x_idle = 0 
        
        self.close()
        
        
        	
def main(): 
   app = QApplication(sys.argv)
   ex = inputdialogdemo()
   ex.show()
   sys.exit(app.exec())
	
if __name__ == '__main__':
   main()

No comments:

Post a Comment