上一篇的GUI版本,使用PyQt5和PyInstaller
1.废话少说,直接上码
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import time
from bluetooth import *
from ctypes import *
import sys
import bluetooth
import base64
import sys
class lock(QMainWindow):
file_changed_signal = pyqtSignal(str) # 信号类型:str
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('蓝牙动态锁')
self.setWindowIcon(QIcon('bluelock.ico'))
self.macedit = QLineEdit()
self.macedit.setInputMask("HH:HH:HH:HH:HH:HH") #设置掩码
self.macedit.setPlaceholderText("请输入蓝牙MAC!")
self.macedit.textChanged.connect(self.getText)
self.file_list = QListWidget()
self.btn = QPushButton('开启自动锁屏')
layout = QGridLayout(self)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
layout.addWidget(self.file_list, 0, 0, 1, 2)
layout.addWidget(self.macedit,1,0,1 ,1)
layout.addWidget(self.btn, 1,1)
self.thread = Worker()
self.thread.file_changed_signal.connect(self.update_file_list)
self.file_changed_signal.connect(self.update_file_list)
self.btn.clicked.connect(self.thread_start)
self.macedit.setFocus()
os.remove("bluelock.ico") #删除图标
def getText(self, text):
global mac #全局mac变量,好处是当return不会用的时候有一个方式变通...
mac=text
def update_file_list(self, file_inf):
self.file_list.addItem(file_inf)
def thread_start(self): #开启进程--目的是防止GUI界面假死
if str((self.macedit.text()))==':::::': #判断是否空输入
self.file_changed_signal.emit('蓝牙MAC地址不能为空!')
print('没有填写蓝牙mac地址')
elif len(str((self.macedit.text())))!=17: #判断长度是否为17位
self.file_changed_signal.emit('没有填写正确蓝牙MAC格式!')
else:
self.btn.setEnabled(False)
self.macedit.setEnabled(False)
self.thread.start()
class Worker(QThread): #进程
file_changed_signal = pyqtSignal(str) # 信号类型:str
def __init__(self):
super().__init__()
self.working = True
def __del__(self):
self.working = False
self.wait()
def run(self):
while True:
self.findDevs() #调用核心函数和10秒休眠
time.sleep(10)
def findDevs(self):
target = mac
notfoundmac = []
err_conn = 0
for x in range(10): #锁屏条件--连续10次找不到
services = bluetooth.find_service(address=target)
if notfoundmac == services:
if mac =='':
self.file_changed_signal.emit('没有填写蓝牙mac地址')
break
err_conn = err_conn + 1
print(err_conn)
self.file_changed_signal.emit('未找到蓝牙地址,序号:%s' %err_conn)
if err_conn == 10:
user32 = windll.LoadLibrary('user32.dll') #锁屏
user32.LockWorkStation()
print('windows is locked!!!')
else:
print('keep alive...')
self.file_changed_signal.emit('keep alive...')
break
#base64转换为ico图标
img_b64_string=b'AAABAAEAICAAAAEAIACoEAAAFgAAACgAAAAgAAAAQAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYAAAAOCQsKFjMrIh4zKyEeCQsKFgAAAA4AAAAGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAjIlGhizc0di0YRQqOCNVdjokVf065JX/+uSV//okVf04I1V2NGEUKiyckdiMSQaGAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEMvIBbFfU2E5Y9W6OqPUv/phkX/6IA7/+d7NP/neTH/53kx/+d7NP/ogDv/6YZF/+qPUv/lj1boxX1MhEIvIBYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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GRf/ndy3/53Ys//vr4P/9+PX/6YVE//vt5P///////PHq/+qNTv/ndy3/53ct/+d3Lf/ndy3/53ct/+h/Ov/nkFbmaU03CgAAAAAAAAAAAAAAAAAAAADUiFRU6o5S/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53cu//nh0v//////++vh/+mFQv/ndiz/++vg//349f/mdiz/65BT//7+/f//////+d/O/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+qPUv/Th1RSAAAAAAAAAAAAAAAAAAAAAOGOVp7phkX/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/6IE+//rm2f//////++nd/+mDQP/76t///fj1/+h9N//53s3///////vr4f/phUP/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/6YZF/+GOVpwAAAAAAAAAAAAAAAABAQEA5pBX0uiAO//ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/6II+//rm2f//////+ufa//zx6v/++vj/+d7N///////77OL/6YZE/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/ogDv/5pBX0gABAQAAAAAAAAAAADVDPgTpklf053s0/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/6II+//rm2f//////////////////////++3j/+mHRv/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+d7NP/pklfyMT05BAAAAAAAAAAAoIRnCuuSV//neTH/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/6II+//rn2v////////////vu5P/ph0f/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53kx/+uSV/+bgWUKAAAAAAAAAACrjW4K65JX/+d5Mf/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/nejH/99bA///////+/v7/+d/O/+h9Nv/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/neTH/65JX/6eLbQoAAAAAAAAAAGR7cgLqkljy53s0/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ox//fUvf//////////////////////+N3K/+d8Nf/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+d7NP/qkljyXHRtAgAAAAAAAAAAEBkYAOmSWNDogDv/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+d5Mf/31L3///////307//99fD//vv6//vu5f//////+NzI/+d8Nf/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndy3/6IA7/+mSWNALDg4AAAAAAAAAAAAAAAAA6ZNZmOmGRf/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/neTH/99O8///////99fH/65FV//vq3//9+PX/6YhH//vu5f//////+NvH/+d8NP/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/phkX/6ZNZmAAAAAAAAAAAAAAAAAAAAADolVxM6o5S/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53cu//fTu////////ffy/+uTWf/ndiz/++rf//349f/mdiz/649T//7+/f//////99bB/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+qPUv/olFxMAAAAAAAAAAAAAAAAAAAAAN+icwbqkljk6H86/+d3Lf/ndy3/53ct/+d3Lf/mdiz/8bSM//349P/sll3/53ct/+d2LP/76+D//fj1/+d8Nf/53cv///////76+P/un2v/53Ys/+d3Lf/ndy3/53ct/+d3Lf/ofzr/6pJY5N+icgYAAAAAAAAAAAAAAAAAAAAAAAAAAOqUWnDqjU//53ct/+d3Lf/ndy3/53ct/+d3Lf/mdi3/6YVD/+d3Lf/ndy3/53Ys//vr4P/++vf/+NjE///////++fb/7Zxm/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+qNT//qlFpwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA5aBtBuqSWNbohEH/53Ys/+d3Lf/ndy3/53ct/+d3Lf/ndy3/53ct/+d3Lf/ndiz/++vh/////////////fj0/+yZYv/ndy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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAvfb0AMXVwgDdsIcI3a+HCMXVwgC38+sAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////////////8A///4AB//8AAP/+AAB//AAAP/gAAB/wAAAP8AAAD+AAAAfgAAAHwAAAA8AAAAPAAAADwAAAA8AAAAPAAAADwAAAA8AAAAPgAAAH4AAAB/AAAA/wAAAP+AAAH/wAAD/+AAB//wAA///AA///8A////////////8='
print(img_b64_string)
convert_img_raw_data = base64.b64decode(img_b64_string)
t = open("bluelock.ico", "wb+")
t.write(convert_img_raw_data)
t.close()
app = QApplication(sys.argv)
dlg = lock()
dlg.show()
sys.exit(app.exec_())