1131022
-待測
使用繁體中文,在w11下使用pytho語言,使用UI界面,新增二個按鈕,一個按下後,可以"隱藏"選擇的資料夾,另一個按下後,可以將被隱藏選擇的資料夾重新顯示回來,預設的畫面大小為400*300,能給完整的示範的程式嗎?
隱藏資料夾:使用 attrib +h +s 將資料夾設為隱藏和系統屬性。
顯示資料夾:使用 attrib -h -s 來移除隱藏和系統屬性。
新增按鈕:增加了一個「列出隱藏資料夾」的按鈕。
列出隱藏資料夾的功能:
使用 os.listdir() 列出選擇資料夾中的所有項目。
檢查每個資料夾的屬性,如果同時具有隱藏和系統屬性,則將其加入列表。
使用 messagebox.showinfo() 顯示找到的隱藏資料夾
使用 simpledialog.askstring:當列出隱藏資料夾後,彈出對話框顯示所有找到的隱藏資料夾,並要求用戶輸入要顯示的資料夾完整路徑。
確認路徑有效性:檢查用戶輸入的路徑是否在找到的隱藏資料夾列表中,並調用 show_folder 函數重新顯示。
1131024-hidefolder-test07
-打包成工作用
import os
import tkinter as tk
from tkinter import filedialog, messagebox, Toplevel
import sys
from tkinter import PhotoImage
def hide_folder_as_system():
folder_path = filedialog.askdirectory()
if folder_path:
try:
result = os.system(f'attrib +h +s "{folder_path}"')
if result == 0:
messagebox.showinfo("成功", "資料夾已隱藏並設為系統項目!")
else:
messagebox.showerror("錯誤", "無法隱藏資料夾。")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def hide_folder_as_normal():
folder_path = filedialog.askdirectory()
if folder_path:
try:
result = os.system(f'attrib +h "{folder_path}"')
if result == 0:
messagebox.showinfo("成功", "資料夾已隱藏為一般資料夾!")
else:
messagebox.showerror("錯誤", "無法隱藏資料夾。")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def show_folder(folder_path):
if folder_path:
try:
result = os.system(f'attrib -h -s "{folder_path}"')
if result == 0:
messagebox.showinfo("成功", "資料夾已顯示!")
else:
messagebox.showerror("錯誤", "無法顯示資料夾。")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def list_hidden_folders():
try:
base_path = filedialog.askdirectory()
if base_path:
hidden_folders = []
for folder in os.listdir(base_path):
full_path = os.path.join(base_path, folder)
if os.path.isdir(full_path):
attrs = os.stat(full_path).st_file_attributes
if attrs & 0x02 and attrs & 0x04: # 隱藏 + 系統
hidden_folders.append(full_path)
if hidden_folders:
show_hidden_folders(hidden_folders)
else:
messagebox.showinfo("結果", "沒有找到隱藏的資料夾。")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def show_hidden_folders(hidden_folders):
window = Toplevel(root)
window.title("隱藏的資料夾")
window.geometry("400x300")
listbox = tk.Listbox(window, selectmode=tk.SINGLE, width=60, height=10)
for folder in hidden_folders:
listbox.insert(tk.END, folder)
listbox.pack(pady=10, fill=tk.BOTH, expand=True)
select_button = tk.Button(window, text="顯示選擇的資料夾", command=lambda: show_folder(listbox.get(listbox.curselection())))
select_button.pack(pady=5)
copy_button = tk.Button(window, text="複製選擇的路徑", command=lambda: copy_to_clipboard(listbox.get(listbox.curselection())))
copy_button.pack(pady=5)
close_button = tk.Button(window, text="關閉", command=window.destroy)
close_button.pack(pady=5)
def copy_to_clipboard(path):
root.clipboard_clear()
root.clipboard_append(path)
messagebox.showinfo("成功", "路徑已複製到剪貼板!")
# 獲取當前檔案所在的路徑
if getattr(sys, 'frozen', False):
current_path = sys._MEIPASS
else:
current_path = os.path.dirname(os.path.abspath(__file__))
root = tk.Tk()
root.title("資料夾隱藏工具")
root.geometry("700x200")
button_style = {
'bg': 'red',
'fg': 'white',
'font': ('Arial', 14)
}
button_frame = tk.Frame(root)
button_frame.pack(pady=10)
hide_button = tk.Button(button_frame, text="隱藏成系統資料夾", command=hide_folder_as_system, **button_style)
hide_button.pack(side=tk.LEFT, padx=5)
hide_normal_button = tk.Button(button_frame, text="隱藏為一般資料夾", command=hide_folder_as_normal, **button_style)
hide_normal_button.pack(side=tk.LEFT, padx=5)
show_button = tk.Button(button_frame, text="顯示資料夾", command=lambda: show_folder(filedialog.askdirectory()), **button_style)
show_button.pack(side=tk.LEFT, padx=5)
list_button = tk.Button(button_frame, text="列出系統隱藏資料夾", command=list_hidden_folders, **button_style)
list_button.pack(side=tk.LEFT, padx=5)
# 圖片加載及錯誤處理
try:
image_path = os.path.join(current_path, "image.png")
image = PhotoImage(file=image_path)
image = image.subsample(4, 4)
label = tk.Label(root, image=image)
label.image = image
label.pack(side=tk.BOTTOM, anchor='sw', padx=10, pady=10)
except Exception as e:
messagebox.showerror("錯誤", "圖片加載失敗: " + str(e))
note_label = tk.Label(root, text="by 資訊組 FSPS-新北市中和區復興國小", font=("Arial", 10), fg="seagreen")
note_label.place(relx=0.99, rely=0.99, anchor='se')
root.mainloop()
1131024-hidefolder-test05
接上,能將按鈕變成同一排顯系,並將按鈕的背景顏色、文字顏色、字型大小用相同的變成統一修正,請協助修正程式
-修正彈出的畫面大小為400*300
import os
import tkinter as tk
from tkinter import filedialog, messagebox, Toplevel
def hide_folder_as_system():
folder_path = filedialog.askdirectory()
if folder_path:
try:
result = os.system(f'attrib +h +s "{folder_path}"')
if result == 0:
messagebox.showinfo("成功", "資料夾已隱藏並設為系統項目!")
else:
messagebox.showerror("錯誤", "無法隱藏資料夾。")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def hide_folder_as_normal():
folder_path = filedialog.askdirectory()
if folder_path:
try:
result = os.system(f'attrib +h "{folder_path}"')
if result == 0:
messagebox.showinfo("成功", "資料夾已隱藏為一般資料夾!")
else:
messagebox.showerror("錯誤", "無法隱藏資料夾。")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def show_folder(folder_path):
if folder_path:
try:
result = os.system(f'attrib -h -s "{folder_path}"')
if result == 0:
messagebox.showinfo("成功", "資料夾已顯示!")
else:
messagebox.showerror("錯誤", "無法顯示資料夾。")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def list_hidden_folders():
try:
base_path = filedialog.askdirectory()
if base_path:
hidden_folders = []
for folder in os.listdir(base_path):
full_path = os.path.join(base_path, folder)
if os.path.isdir(full_path):
attrs = os.stat(full_path).st_file_attributes
if attrs & 0x02 and attrs & 0x04: # 隱藏 + 系統
hidden_folders.append(full_path)
if hidden_folders:
show_hidden_folders(hidden_folders)
else:
messagebox.showinfo("結果", "沒有找到隱藏的資料夾。")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def show_hidden_folders(hidden_folders):
window = Toplevel(root)
window.title("隱藏的資料夾")
# 設置視窗大小為 400x300
window.geometry("400x300")
# 調整 Listbox 大小以適應視窗
listbox = tk.Listbox(window, selectmode=tk.SINGLE, width=60, height=10) # 調整寬度和高度
for folder in hidden_folders:
listbox.insert(tk.END, folder)
listbox.pack(pady=10, fill=tk.BOTH, expand=True) # 使用 fill 和 expand 讓它填滿視窗
select_button = tk.Button(window, text="顯示選擇的資料夾", command=lambda: show_folder(listbox.get(listbox.curselection())))
select_button.pack(pady=5)
copy_button = tk.Button(window, text="複製選擇的路徑", command=lambda: copy_to_clipboard(listbox.get(listbox.curselection())))
copy_button.pack(pady=5)
#old
#listbox = tk.Listbox(window, selectmode=tk.SINGLE)
#for folder in hidden_folders:
# listbox.insert(tk.END, folder)
#listbox.pack(pady=10)
#select_button = tk.Button(window, text="顯示選擇的資料夾", command=lambda: show_folder(listbox.get(listbox.curselection())))
#select_button.pack(pady=10)
#copy_button = tk.Button(window, text="複製選擇的路徑", command=lambda: copy_to_clipboard(listbox.get(listbox.curselection())))
#copy_button.pack(pady=10)
def copy_to_clipboard(path):
root.clipboard_clear()
root.clipboard_append(path)
messagebox.showinfo("成功", "路徑已複製到剪貼板!")
root = tk.Tk()
root.title("資料夾隱藏工具")
root.geometry("700x200")
# 按鈕樣式設定
button_style = {
'bg': 'red', # 背景顏色
'fg': 'white', # 文字顏色
'font': ('Arial', 14) # 字型和字型大小
}
# 按鈕在同一排
button_frame = tk.Frame(root)
button_frame.pack(pady=10)
hide_button = tk.Button(button_frame, text="隱藏成系統資料夾", command=hide_folder_as_system, **button_style)
hide_button.pack(side=tk.LEFT, padx=5)
hide_normal_button = tk.Button(button_frame, text="隱藏為一般資料夾", command=hide_folder_as_normal, **button_style)
hide_normal_button.pack(side=tk.LEFT, padx=5)
show_button = tk.Button(button_frame, text="顯示資料夾", command=lambda: show_folder(filedialog.askdirectory()), **button_style)
show_button.pack(side=tk.LEFT, padx=5)
list_button = tk.Button(button_frame, text="列出系統隱藏資料夾", command=list_hidden_folders, **button_style)
list_button.pack(side=tk.LEFT, padx=5)
root.mainloop()
1131024-hidefolder-test04
-四個功能,隱藏為一般資料夾、隱藏為系統資料夾、顯示成一般資料夾、列出隱藏資料夾(系統資料夾)
-
import os
import tkinter as tk
from tkinter import filedialog, messagebox, Toplevel
def hide_folder_as_system():
folder_path = filedialog.askdirectory()
if folder_path:
try:
result = os.system(f'attrib +h +s "{folder_path}"')
if result == 0:
messagebox.showinfo("成功", "資料夾已隱藏並設為系統項目!")
else:
messagebox.showerror("錯誤", "無法隱藏資料夾。")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def hide_folder_as_normal():
folder_path = filedialog.askdirectory()
if folder_path:
try:
result = os.system(f'attrib +h "{folder_path}"')
if result == 0:
messagebox.showinfo("成功", "資料夾已隱藏為一般資料夾!")
else:
messagebox.showerror("錯誤", "無法隱藏資料夾。")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def show_folder(folder_path):
if folder_path:
try:
result = os.system(f'attrib -h -s "{folder_path}"')
if result == 0:
messagebox.showinfo("成功", "資料夾已顯示!")
else:
messagebox.showerror("錯誤", "無法顯示資料夾。")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def list_hidden_folders():
try:
base_path = filedialog.askdirectory()
if base_path:
hidden_folders = []
for folder in os.listdir(base_path):
full_path = os.path.join(base_path, folder)
if os.path.isdir(full_path):
attrs = os.stat(full_path).st_file_attributes
if attrs & 0x02 and attrs & 0x04: # 隱藏 + 系統
hidden_folders.append(full_path)
if hidden_folders:
show_hidden_folders(hidden_folders)
else:
messagebox.showinfo("結果", "沒有找到隱藏的資料夾。")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def show_hidden_folders(hidden_folders):
window = Toplevel(root)
window.title("隱藏的資料夾")
listbox = tk.Listbox(window, selectmode=tk.SINGLE)
for folder in hidden_folders:
listbox.insert(tk.END, folder)
listbox.pack(pady=10)
select_button = tk.Button(window, text="顯示選擇的資料夾", command=lambda: show_folder(listbox.get(listbox.curselection())))
select_button.pack(pady=10)
copy_button = tk.Button(window, text="複製選擇的路徑", command=lambda: copy_to_clipboard(listbox.get(listbox.curselection())))
copy_button.pack(pady=10)
def copy_to_clipboard(path):
root.clipboard_clear()
root.clipboard_append(path)
messagebox.showinfo("成功", "路徑已複製到剪貼板!")
root = tk.Tk()
root.title("資料夾隱藏工具")
root.geometry("400x300")
hide_button = tk.Button(root, text="隱藏成系統資料夾", command=hide_folder_as_system)
hide_button.pack(pady=10)
hide_normal_button = tk.Button(root, text="隱藏為一般資料夾", command=hide_folder_as_normal)
hide_normal_button.pack(pady=10)
show_button = tk.Button(root, text="顯示資料夾", command=lambda: show_folder(filedialog.askdirectory()))
show_button.pack(pady=10)
list_button = tk.Button(root, text="列出隱藏資料夾", command=list_hidden_folders)
list_button.pack(pady=10)
root.mainloop()
import os
import tkinter as tk
from tkinter import filedialog, messagebox, simpledialog, Toplevel
def hide_folder():
folder_path = filedialog.askdirectory()
if folder_path:
try:
os.system(f'attrib +h +s "{folder_path}"')
messagebox.showinfo("成功", "資料夾已隱藏並設為系統項目!")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def show_folder(folder_path):
if folder_path:
try:
os.system(f'attrib -h -s "{folder_path}"')
messagebox.showinfo("成功", "資料夾已顯示!")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def list_hidden_folders():
try:
# 指定要檢查的路徑
base_path = filedialog.askdirectory()
if base_path:
hidden_folders = []
for folder in os.listdir(base_path):
full_path = os.path.join(base_path, folder)
if os.path.isdir(full_path):
attrs = os.stat(full_path).st_file_attributes
if attrs & 0x02 and attrs & 0x04: # 隱藏 + 系統
hidden_folders.append(full_path)
if hidden_folders:
show_hidden_folders(hidden_folders)
else:
messagebox.showinfo("結果", "沒有找到隱藏的資料夾。")
except Exception as e:
messagebox.showerror("錯誤", str(e))
def show_hidden_folders(hidden_folders):
# 建立新窗口顯示隱藏資料夾
window = Toplevel(root)
window.title("隱藏的資料夾")
listbox = tk.Listbox(window, selectmode=tk.SINGLE)
for folder in hidden_folders:
listbox.insert(tk.END, folder)
listbox.pack(pady=10)
def on_select(event):
selected_folder = listbox.get(listbox.curselection())
show_folder(selected_folder)
select_button = tk.Button(window, text="顯示選擇的資料夾", command=on_select)
select_button.pack(pady=10)
copy_button = tk.Button(window, text="複製選擇的路徑", command=lambda: copy_to_clipboard(listbox.get(listbox.curselection())))
copy_button.pack(pady=10)
def copy_to_clipboard(path):
root.clipboard_clear() # 清空剪貼板
root.clipboard_append(path) # 添加新的路徑到剪貼板
messagebox.showinfo("成功", "路徑已複製到剪貼板!")
# 建立主窗口
root = tk.Tk()
root.title("資料夾隱藏工具")
root.geometry("400x300")
# 建立按鈕
hide_button = tk.Button(root, text="隱藏資料夾", command=hide_folder)
hide_button.pack(pady=10)
show_button = tk.Button(root, text="顯示資料夾", command=lambda: show_folder(filedialog.askdirectory()))
show_button.pack(pady=10)
list_button = tk.Button(root, text="列出隱藏資料夾", command=list_hidden_folders)
list_button.pack(pady=10)
# 啟動主事件循環
root.mainloop()