[Python] 纯文本查看 复制代码
from tkinter import END, Checkbutton, Listbox, Tk, Label, Button, filedialog, PhotoImage
from tkinter.messagebox import askokcancel
from ctypes import windll
from wmi import WMI
from os import _exit, path
from time import sleep
from threading import Thread
from pythoncom import CoInitialize
from pystray import MenuItem, Icon, Menu
from PIL import Image
# 状态栏图标
def icon():
def show_window():
global is_w
# 首次启动不显示窗口
if is_w == 0:
Thread(target=window).start()
is_w = 1
else:
root_windows.deiconify()
menu = (MenuItem('设置', show_window), MenuItem('退出', exit),
Menu.SEPARATOR)
image = Image.open("icon.png")
icon = Icon("icon", image, "授权已开启", menu)
icon.run()
# 设置主窗口
def window():
global is_update
global root_windows, listbox1, listbox2, listbox3, button1, button2, button3, button4, checkbutton
root_windows = Tk()
# 窗口大小,设置屏幕居中
screenwidth = root_windows.winfo_screenwidth()
screenheight = root_windows.winfo_screenheight()
root_windows.geometry(
f"419x260+{int((screenwidth-419)/2)}+{int((screenheight-236)/2)}")
root_windows.resizable(False, False)
root_windows.iconphoto(False, PhotoImage(file=f'{work_path}\\icon.png'))
root_windows.title("Lock_Screen_v1.1 by ifdess from 52pj.cn")
# 标签
lable1 = Label(root_windows, text="未授权U盘:")
lable1.place(x=20, y=10)
# 标签
lable2 = Label(root_windows, text="已授权U盘:")
lable2.place(x=255, y=10)
# 未授权列表框
listbox1 = Listbox(root_windows)
u_now = get_u()
for item in u_yes:
try:
u_now.remove(item)
except:
pass
for i in u_now:
listbox1.insert(END, i)
listbox1.place(x=20, y=35)
# 已授权列表框
listbox2 = Listbox(root_windows)
for i in u_yes:
listbox2.insert(END, i)
listbox2.place(x=255, y=35)
# 文件模式复选框
checkbutton = Checkbutton(root_windows,
text="指定文件授权", command=select_file)
checkbutton.place(x=16, y=226)
# 指定文件列表框
listbox3 = Listbox(root_windows, height=1, width=39)
if file_yes:
listbox3.insert(END, file_yes)
checkbutton.select()
else:
pass
listbox3.place(x=120, y=228)
# 添加按钮
button1 = Button(root_windows, text="添加 >>",
height=1, width=8, command=add)
button1.place(x=177, y=42)
# 删除按钮
button2 = Button(root_windows, text=" << 删除",
height=1, width=8, command=delete)
button2.place(x=177, y=89)
# 保存按钮
button3 = Button(root_windows, text="保存", height=1,
width=8, command=save_config)
button3.place(x=177, y=136)
# 退出按钮
button4 = Button(root_windows, text="退出", height=1, width=8, command=exit)
button4.place(x=177, y=183)
if is_update == 0:
Thread(target=update_u, daemon=True).start()
is_update = 1
# 设置叉是隐藏窗口
if path.exists(f"{work_path}/config.ini"):
root_windows.protocol("WM_DELETE_WINDOW", root_windows.withdraw)
else:
root_windows.protocol("WM_DELETE_WINDOW", exit)
root_windows.mainloop()
work_path = path.dirname(path.abspath(__file__))
# 保存配置文件
def save_config():
global u_yes, file_yes
with open(f"{work_path}\\config.ini", "w") as c:
c.write(str(u_yes)+"\n")
c.write(str(file_yes))
askokcancel(title="通知", message="配置保存成功。")
# 读取配置文件
def read_config():
global u_yes, file_yes
if path.exists(f"{work_path}/config.ini"):
with open(f"{work_path}\\config.ini", "r") as c:
u_yes = eval(c.readline())
file_yes = c.readline()
else:
u_yes = get_u()
file_yes = ""
# 获取U盘型号和序列号,保存为列表
def get_u(var=[]):
var = []
CoInitialize()
for item in WMI().Win32_DiskDrive():
if item.MediaType == "Removable Media":
var.append(item.Model+" "+item.SerialNumber)
return var
# 删除选中项
def delete():
global u_yes, listbox1, listbox2
try:
u_yes.remove(listbox2.get(listbox2.curselection()))
listbox2.delete(listbox2.curselection())
listbox1.delete(0, END)
except:
pass
# 添加选中项
def add():
global u_yes, listbox1, listbox2
u_yes.append(listbox1.get(listbox1.curselection()))
listbox2.insert(END, (listbox1.get(listbox1.curselection())))
listbox1.delete(listbox1.curselection())
# 核心判断程序,是否屏蔽
def run():
global is_w
while True:
if get_u() == []:
windll.user32.BlockInput(True)
else:
# 判断实时获取的U盘列表和刚开机时的U盘列表是否有交集,如果有就代表教师U盘仍旧保持插入
if list(set(get_u()).intersection(u_yes)) == []:
if is_file():
windll.user32.BlockInput(False)
else:
windll.user32.BlockInput(True)
else:
windll.user32.BlockInput(False)
sleep(0.001)
# 打开选择文件的窗口,并处理文件路径
def select_file():
global select_path, listbox3, file_yes, CheckBox
if len(file_yes) != 0:
file_yes = ""
listbox3.delete(0, END)
else:
selected_file_path = filedialog.askopenfilename()
file_yes = selected_file_path[2:]
if len(file_yes) == 0:
checkbutton.deselect()
else:
listbox3.delete(0, END)
listbox3.insert(END, selected_file_path[2:])
file_yes = selected_file_path[2:]
checkbutton.select()
# 遍历所有盘路径,判断授权文件是否存在
def is_file():
global file_yes
if len(file_yes) == 0:
return False
else:
for item in [chr(abc) for abc in range(97, 123)]:
if path.exists(f"{item}:{file_yes}"):
return True
def exit():
_exit(0)
# 更新显示列表
def update_u():
global listbox1
u_temp = []
while True:
u_now = get_u()
for item in u_yes:
try:
u_now.remove(item)
except:
pass
if u_now != u_temp and u_now != None:
listbox1.delete(0, END)
u_temp = u_now
for item in u_now:
listbox1.insert(END, item)
else:
pass
sleep(0.001)
# 程序开始
read_config()
if path.exists(f"{work_path}/config.ini"):
is_w = 0
is_update = 0
Thread(target=icon).start()
Thread(target=run).start()
else:
is_update = 0
window()