1131023修正
-打包檔案
pyinstaller --onefile --windowed --add-data "image.png;." your_script.py
--onefile:這個選項會將所有檔案打包成一個可執行檔案。
--windowed:這個選項會禁止顯示命令提示字元窗口(適用於 GUI 應用)。
note_label = tk.Label(root, text="Copyright Ⓒ2025 資訊組王清賢 FSPS-新北市中和區復興國小", font=("Arial", 12), fg="dodgerblue")
note_label.place(relx=0.99, rely=0.99, anchor='se')
文字顯示方式
1.
# 使用 Canvas 顯示文字
canvas = tk.Canvas(root, width=400, height=100)
canvas.pack()
# 在 Canvas 上畫文字
canvas.create_text(200, 50, text="這是 Canvas 上的文字說明", font=("Arial", 14), fill="black")
2.
# 使用 Message 顯示較長的文字說明
message = tk.Message(root, text="這是較長的文字說明。這是一段多行文字,用來展示 Message 控制元件。", width=300) message.pack(pady=20)
3.
# 建立一個 Label 來顯示文字
label = tk.Label(root, text="這是文字說明:請點擊下方按鈕", font=("Arial", 12), fg="black") label.pack(pady=20)
import os
import sys
import tkinter as tk
from tkinter import messagebox
def resource_path(relative_path):
"""獲取資源的絕對路徑,不論是在開發環境還是被打包後"""
try:
base_path = sys._MEIPASS
except AttributeError:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
# Function to switch displays using DisplaySwitch.exe
def switch_display(option):
commands = {
"PC screen only": "DisplaySwitch.exe /internal",
"Duplicate": "DisplaySwitch.exe /clone",
"Extend": "DisplaySwitch.exe /extend",
"Second screen only": "DisplaySwitch.exe /external"
}
if option in commands:
os.system(commands[option])
# Initialize the main window
window = tk.Tk()
window.title("螢幕切換方式 by WCS 2024.10.07")
# Set window size
window.geometry("400x350")
# Create and configure a frame
frame = tk.Frame(window, padx=20, pady=20)
frame.pack()
# Create buttons for each display mode
options = ["僅電腦螢幕", "同步顯示", "延伸", "僅第二個螢幕"]
for option in options:
display_option = {
"僅電腦螢幕": "PC screen only",
"同步顯示": "Duplicate",
"延伸": "Extend",
"僅第二個螢幕": "Second screen only"
}[option]
button = tk.Button(frame, text=option, width=20, command=lambda o=display_option: switch_display(o), fg="blue", font=("Arial", 16)) # 設定文字顏色及字體大小
button.pack(pady=5)
# 新增備註標籤
note_label = tk.Label(window, text="by 資訊組 FSPS-新北市中和區復興國小", font=("Arial", 10), fg="black")
note_label.place(relx=0.99, rely=0.99, anchor='se') # 右下角對齊
# 載入並顯示圖片
img_path = resource_path("image.png") # 使用 resource_path 獲取正確路徑
original_image = tk.PhotoImage(file=img_path) # 替換為你的圖片檔名
image = original_image.subsample(4, 4) # 調整大小,這裡將圖片縮小為原來的 1/4
image_label = tk.Label(window, image=image)
image_label.place(relx=0.01, rely=0.99, anchor='sw') # 左下角對齊
# Run the Tkinter event loop
window.mainloop()