历尽千辛万苦,在神秘大佬的帮助下,也是成功找回文件啦!
好啦,全篇代码近1000行,本来是想分开写的,但是后面觉得太麻烦了,就直接写一起了
import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk
import datetime
import time
import subprocess
# 创建窗口
w = tk.Tk()
w.title('My-OS')
w.geometry('1000x650')
w.resizable(False, False)
second_state = None
messagebox.showerror('提示','请不要直接运行代码,内部文件的路径需要手动更改,不然会报错!!!!')
def login_win():
# 解除绑定
w.unbind('<Key>') # 解除键盘按键事件
w.unbind('<Button-1>') # 解除鼠标左键点击事件
'''
同理,如果下面绑定了鼠标的中键和右键,这里就需要解除绑定
w.unbind('<Button-2>') # 解除鼠标中键点击事件 (在某些系统上可能是 Button-3)
w.unbind('<Button-3>') # 解除鼠标右键点击事件
'''
# 锁屏壁纸显示
img = Image.open(r'C:\Users\29507\Desktop\操作系统\锁屏壁纸\feature_16.jpg')
img = img.resize((1000,650), Image.LANCZOS)
lock_screen_wallpapers = ImageTk.PhotoImage(img)
lock_screen_wallpapers_label = tk.Label(w, image=lock_screen_wallpapers)
lock_screen_wallpapers_label.image = lock_screen_wallpapers
lock_screen_wallpapers_label.place(x=0, y=0, relwidth=1, relheight=1)
# 头像显示
img = Image.open(r'C:\Users\29507\Desktop\操作系统\头像\头像.jpg')
img = img.resize((150,150), Image.LANCZOS)
Avatar_image = ImageTk.PhotoImage(img)
Avatar_frame = tk.Label(w, image=Avatar_image)
Avatar_frame.image = Avatar_image
Avatar_frame.pack(side='top', pady=150)
# 昵称显示
name_text = '林梦璃'
name_label = tk.Label(w, text=name_text,font=('微软雅黑 16'))
name_label.place(x=465, y=310)
# 密码输入框显示
password_entry = tk.Entry(w, show='*', width=22, justify='center')
password_entry.place(x=420, y=350)
# 登录、忘记密码、登录选项label
check_label = tk.Label(w, text='确认', font=('微软雅黑 16'))
check_label.place(x=475, y=380)
check_label.bind("<Button-1>", lambda event: check_login())
forgot_label = tk.Label(w, text='忘记密码?', font=("微软雅黑 16"))
forgot_label.place(x=454, y=410)
sign_in_options_label = tk.Label(w,text='登录选项', font=('微软雅黑 16'))
sign_in_options_label.place(x=454, y=440)
def check_login():
password_get = password_entry.get()
# 判断密码,正确即进入系统,不正确则提示相关信息
if password_get == '123456' or password_get == 'admin' or password_get == "":
# 密码正确,可以继续执行后续操作
Avatar_frame.place_forget() # 隐藏头像
name_label.place_forget() # 隐藏昵称
password_entry.delete(0, 'end') # 清空密码输入框
check_label.place_forget() # 隐藏确认按钮
forgot_label.place_forget() # 隐藏忘记密码标签
sign_in_options_label.place_forget() # 隐藏登录选项标签
# 设置缓冲时间
time.sleep(2)
# 定义壁纸文件名列表
wallpaper_files = [
r"C:\Users\29507\Desktop\操作系统\壁纸\壁纸1.jpg",
r"C:\Users\29507\Desktop\操作系统\壁纸\壁纸2.jpg",
r"C:\Users\29507\Desktop\操作系统\壁纸\壁纸3.jpg",
r"C:\Users\29507\Desktop\操作系统\壁纸\壁纸4.jpg"
]
# 当前壁纸索引
current_wallpaper_index = 0
# 加载初始壁纸
img = Image.open(wallpaper_files[current_wallpaper_index])
img = img.resize((1000, 650), Image.LANCZOS) # 保持图片宽高比不变调整大小
bg_image = ImageTk.PhotoImage(img)
# 创建 Label 并设置图片为背景
label = tk.Label(w, image=bg_image)
label.place(x=0, y=0, relwidth=1, relheight=1)
# 这里保持对 PhotoImage 的引用,避免被垃圾回收
label.image = bg_image
# 定义更改壁纸的功能
def change_wallpaper():
global current_wallpaper_index
# 更新壁纸索引
current_wallpaper_index = (current_wallpaper_index + 1) % len(wallpaper_files)
wallpaper_file = wallpaper_files[current_wallpaper_index]
# 加载新壁纸
img = Image.open(wallpaper_file)
img = img.resize((1000, 650), Image.LANCZOS) # 保持图片宽高比不变调整大小
bg_image = ImageTk.PhotoImage(img)
label.config(image=bg_image)
label.image = bg_image # 保持对 PhotoImage 的引用,避免被垃圾回收
# 定义切换主题的函数
def toggle_theme():
answer = messagebox.askyesno("主题切换", "您确定要切换到深色模式吗?")
if answer:
set_dark_theme()
else:
set_light_theme()
# 定义设置深色主题的函数
def set_dark_theme():
label.config(bg='black')
for button in buttons:
button.config(bg='black')
# 定义设置浅色主题的函数
def set_light_theme():
label.config(bg='white')
for button in buttons:
button.config(bg='white')
# 定义刷新桌面的功能
def refresh_desktop():
# 重置图标大小为默认(中等)
change_icon_size("medium")
# 建立两个字典,用于存储按钮坐标位置
new_Button_position = {'1':'None','2':'None','3':'None','4':[10,275],'5':[10,365],'6':[10,455],
'7':[85,10],'8':[85,95],'9':[85,185],'10':[85,275],'11':[85,365],'12':[85,455],
'13':[155,10],'14':[155,95],'15':[155,185],'16':[155,275],'17':[155,365],'18':[155,455]}
# new_Button_position 用于存储未使用的按钮坐标
old_Button_position = {'1':[10,10],'2':[10,95],'3':[10,185],'4':'None','5':'None','6':'None',
'7':'None','8':'None','9':'None','10':'None','11':'None','12':'None',
'13':'None','14':'None','15':'None','16':'None','17':'None','18':'None',}
# old_Button_position 用于存储已使用的按钮坐标
# 定义新建文件夹的功能
def Create_a_new_folder():
global last_button_y # 全局变量用于跟踪最后一个按钮的 y 坐标
# 加载新建文件夹图片
new_folder_button_img = Image.open(r"C:\Users\29507\Desktop\操作系统\应用图标\新建文件夹.png")
new_folder_button_img = new_folder_button_img.resize((icon_width, icon_height), Image.LANCZOS) # 调整图片大小
new_folder_button_image = ImageTk.PhotoImage(new_folder_button_img)
# 创建新建文件夹 Button 并设置图片和文字
new_folder_button = tk.Button(w, image=new_folder_button_image, text="新建文件夹", compound=tk.TOP, anchor=tk.NW,
bg=label.cget("bg"), # 设置按钮背景颜色为与 Label 背景相同
borderwidth=0, # 设置边框宽度为0
highlightthickness=0) # 设置高亮边框厚度为0
new_folder_button.image = new_folder_button_image # 保持对 PhotoImage 的引用,避免被垃圾回收
new_folder_button.image_path = r"C:\Users\29507\Desktop\操作系统\应用图标\新建文件夹.png" # 保存原始图像文件路径
# 找到第一个可用的位置
for key in new_Button_position:
if isinstance(new_Button_position[key], list): # 检查是否为可用位置
x, y = new_Button_position[key]
new_folder_button.place(x=x, y=y)
break
# 更新字典
for key in new_Button_position:
if new_folder_button.place_info().get('x') == str(new_Button_position[key][0]) and \
new_folder_button.place_info().get('y') == str(new_Button_position[key][1]):
new_Button_position[key] = 'None' # 标记为已使用
old_Button_position[key] = [new_folder_button.place_info()['x'], new_folder_button.place_info()['y']]
break
# 绑定右键点击事件
new_folder_button.unbind("<Button-3>") # 取消之前的绑定
new_folder_button.bind("<Button-3>", lambda event: show_popup(menu_app, event)) # 使用应用菜单
# 将新按钮添加到按钮列表中
buttons.append(new_folder_button)
# 创建一个弹出菜单
menu_app = tk.Menu(label, tearoff=0) # 用于应用软件的右键菜单
# 应用软件右键菜单项
menu_app.add_command(label="打开", command=lambda: print("打开了应用"))
menu_app.add_command(label="属性", command=lambda: print("查看了应用的属性"))
menu_app.add_separator()
menu_app.add_command(label="删除", command=lambda: print("删除了应用"))
# 创建另一个弹出菜单
menu = tk.Menu(label, tearoff=0) # 用于桌面区域的右键菜单
# 桌面区域右键菜单项
menu.add_command(label="新建文件夹", command=Create_a_new_folder) # 新建文件夹
menu.add_command(label="更改壁纸", command=change_wallpaper) # 更改壁纸
menu.add_separator()
menu.add_command(label="刷新桌面", command=refresh_desktop) # 刷新桌面
# 创建查看子菜单
view_menu = tk.Menu(menu, tearoff=0)
view_menu.add_command(label="大图标", command=lambda: change_icon_size("large"))
view_menu.add_command(label="中等图标", command=lambda: change_icon_size("medium"))
view_menu.add_command(label="小图标", command=lambda: change_icon_size("small"))
menu.add_cascade(label="查看", menu=view_menu)
# 创建排序方式子菜单
sort_menu = tk.Menu(menu, tearoff=0)
sort_menu.add_command(la