使用繁體中文,在w11下使用pytho語言,使用UI界面,新增一個按鈕,按下後,要能查詢本機系統的授權狀況呈現在UI上,能給示範的程式嗎?
接上,如果要新增一個"查詢office授權"的功能,要如何修正,放在原功能的右邊
1131021-sysinfo-03_01
查詢 Office 授權的功能:新增了一個 check_office_activation_status 函數,使用 ospp.vbs 來查詢 Office 授權狀況。
注意:ospp.vbs 的路徑可能依據你的 Office 版本不同而有所不同。在這裡,我假設你安裝的是 Office 2016,路徑為 C:\Program Files\Microsoft Office\Office16\ospp.vbs。
如果你使用的是不同版本的 Office,請根據需要調整路徑。
UI 按鈕:在原有的按鈕旁邊新增了一個按鈕來查詢 Office 授權,並將兩個按鈕放置在同一個框架 (Frame) 中,並設置 side=tk.LEFT 以便並排顯示。
確保 ospp.vbs 的路徑正確,如果你的 Office 版本不同,請調整路徑。
執行時仍需確保以管理員身份運行。
import tkinter as tk
from tkinter import messagebox
import subprocess
import os
def check_activation_status():
try:
slmgr_path = os.path.join(os.environ['SystemRoot'], 'System32', 'slmgr.vbs')
result = subprocess.run(['cscript', slmgr_path, '/xpr'], capture_output=True, text=True)
output = result.stdout.strip()
status_label.config(text=output)
except Exception as e:
messagebox.showerror("錯誤", str(e))
def check_office_activation_status():
try:
# 使用完整路徑來執行 cscript 查詢 Office 授權
office_script_path = os.path.join(os.environ['ProgramFiles'], 'Microsoft Office', 'Office16', 'ospp.vbs')
result = subprocess.run(['cscript', office_script_path, '/dstatus'], capture_output=True, text=True)
output = result.stdout.strip()
status_label.config(text=output)
except Exception as e:
messagebox.showerror("錯誤", str(e))
# 建立主窗口
root = tk.Tk()
root.title("授權狀況查詢")
#root.geometry("400x300")
# 建立框架以放置按鈕
button_frame = tk.Frame(root)
button_frame.pack(pady=20)
# 查詢 Windows 授權狀況的按鈕
check_button = tk.Button(button_frame, text="查詢 Windows 授權", command=check_activation_status)
check_button.pack(side=tk.LEFT, padx=5)
# 查詢 Office 授權狀況的按鈕
check_office_button = tk.Button(button_frame, text="查詢 Office 授權", command=check_office_activation_status)
check_office_button.pack(side=tk.LEFT, padx=5)
# 顯示授權狀況的標籤
status_label = tk.Label(root, text="授權狀況將顯示在這裡")
status_label.pack(pady=20)
# 開始主循環
root.mainloop()
1131021-sysinfo-03_02
新增按鈕:我們在窗口中新增了一個按鈕 office_check_button,用於查詢 Office 授權狀況,並放置在左邊的按鈕旁邊。
查詢 Office 授權:使用 ospp.vbs 指令來查詢 Office 的授權狀況,這個檔案通常位於 C:\Program Files\Microsoft Office\Office16\(根據安裝版本可能會有所不同)。
顯示標籤:新增了一個標籤 office_status_label 來顯示 Office 的授權狀況。
如果 Office 的安裝路徑不同,請根據實際情況修改 office_path。
確保你以管理員身份運行這個程式,以避免權限問題。
這樣一來,當你按下「查詢 Office 授權狀況」的按鈕時,就會顯示相關的授權狀況。如果有任何疑問或需要進一步的調整,隨時告訴我!
import tkinter as tk
from tkinter import messagebox
import subprocess
import os
def check_activation_status():
try:
slmgr_path = os.path.join(os.environ['SystemRoot'], 'System32', 'slmgr.vbs')
result = subprocess.run(['cscript', slmgr_path, '/xpr'], capture_output=True, text=True)
output = result.stdout.strip()
status_label.config(text=output)
except Exception as e:
messagebox.showerror("錯誤", str(e))
def check_office_activation_status():
try:
# 使用 Office 的執行檔來查詢授權狀況
office_path = os.path.join(os.environ['ProgramFiles'], 'Microsoft Office', 'Office16', 'ospp.vbs')
result = subprocess.run(['cscript', office_path, '/dstatus'], capture_output=True, text=True)
output = result.stdout.strip()
office_status_label.config(text=output)
except Exception as e:
messagebox.showerror("錯誤", str(e))
# 建立主窗口
root = tk.Tk()
root.title("授權狀況查詢")
# 查詢 Windows 授權狀況的按鈕
check_button = tk.Button(root, text="查詢 Windows 授權狀況", command=check_activation_status)
check_button.pack(side=tk.LEFT, padx=10, pady=20)
# 查詢 Office 授權狀況的按鈕
office_check_button = tk.Button(root, text="查詢 Office 授權狀況", command=check_office_activation_status)
office_check_button.pack(side=tk.LEFT, padx=10, pady=20)
# 顯示 Windows 授權狀況的標籤
status_label = tk.Label(root, text="Windows 授權狀況將顯示在這裡")
status_label.pack(pady=20)
# 顯示 Office 授權狀況的標籤
office_status_label = tk.Label(root, text="Office 授權狀況將顯示在這裡")
office_status_label.pack(pady=20)
# 開始主循環
root.mainloop()
多線程處理
import tkinter as tk
from tkinter import messagebox
import subprocess
import os
import threading
def run_command(command, label):
try:
result = subprocess.run(command, capture_output=True, text=True)
output = result.stdout.strip()
label.config(text=output)
except Exception as e:
messagebox.showerror("錯誤", str(e))
def check_activation_status():
slmgr_path = os.path.join(os.environ['SystemRoot'], 'System32', 'slmgr.vbs')
command = ['cscript', slmgr_path, '/xpr']
threading.Thread(target=run_command, args=(command, status_label)).start()
def check_office_activation_status():
office_path = os.path.join(os.environ['ProgramFiles'], 'Microsoft Office', 'Office16', 'ospp.vbs')
command = ['cscript', office_path, '/dstatus']
threading.Thread(target=run_command, args=(command, office_status_label)).start()
def check_office_product_key():
office_path = os.path.join(os.environ['ProgramFiles'], 'Microsoft Office', 'Office16', 'ospp.vbs')
command = ['cscript', office_path, '/dkey']
threading.Thread(target=run_command, args=(command, product_key_label)).start()
# 建立主窗口
root = tk.Tk()
root.title("授權狀況查詢")
# 查詢 Windows 授權狀況的按鈕
check_button = tk.Button(root, text="查詢 Windows 授權狀況", command=check_activation_status)
check_button.pack(side=tk.LEFT, padx=10, pady=20)
# 查詢 Office 授權狀況的按鈕
office_check_button = tk.Button(root, text="查詢 Office 授權狀況", command=check_office_activation_status)
office_check_button.pack(side=tk.LEFT, padx=10, pady=20)
# 查詢 Office 授權金鑰的按鈕
key_check_button = tk.Button(root, text="查詢 Office 授權金鑰", command=check_office_product_key)
key_check_button.pack(side=tk.LEFT, padx=10, pady=20)
# 顯示 Windows 授權狀況的標籤
status_label = tk.Label(root, text="Windows 授權狀況將顯示在這裡")
status_label.pack(pady=20)
# 顯示 Office 授權狀況的標籤
office_status_label = tk.Label(root, text="Office 授權狀況將顯示在這裡")
office_status_label.pack(pady=20)
# 顯示 Office 授權金鑰的標籤
product_key_label = tk.Label(root, text="Office 授權金鑰將顯示在這裡")
product_key_label.pack(pady=20)
# 開始主循環
root.mainloop()