#記住在cmd中運行,不要在vscode里運行,否則env會裝到工程目錄下
python -m venv env
#env\Scripts\activate.bat
pip install pyside6
下載本期源碼
vscode裝一個PYQT Integration插件,設置好兩個路徑(下面有個腳本用于獲取路徑)
用everything的童鞋注意了:工具/選項/索引/強制重建
重啟vscode可以看到,右擊.ui文件時出現可以操作的菜單
我們New一個Form默認生成一個.ui文件,然后點擊編譯,就會出現我們需要的Ui_untitled.py,這就是編譯出來的文件。在test.py中輸入以下代碼F5運行,對話框就出現了
import sys
from Ui_untitled import Ui_Dialog
from PySide6.QtWidgets import QApplication, QDialog # 假設您使用的是 PyQt5class MyDialog(QDialog):def __init__(self):super(MyDialog, self).__init__()self.ui = Ui_Dialog() # 假設 Ui_Dialog 是您的 UI 文件生成的類self.ui.setupUi(self) # 將當前對話框實例傳遞給 setupUi 方法if __name__ == "__main__":app = QApplication([])win = MyDialog()win.show()app.exec_()
最后附上一段獲取exe文件路徑的腳本,方便路徑的復制粘貼:(注意不要重復運行,里面有添加環境變量操作,重復運行會添加重復的環境變量)
import os
import sys
import subprocess
from pathlib import Path
from win32com.client import Dispatch #要安裝依賴:python -m pip install pypiwin32# 1. 搜索Python目錄并設置pkgDir變量
def find_python_directory():for root, dirs, files in os.walk('C:/Users/Administrator/AppData/Local/Programs'):if 'python.exe' in files:return Path(root)return Nonepython_dir = find_python_directory()
if python_dir is None:print("Python not found.")sys.exit(1)pkg_dir = python_dir / 'Lib' / 'site-packages'
print(f"Found Python at: {python_dir}")
print(f"Package directory: {pkg_dir}")# 2. 在pkgDir中搜索Designer.exe并創建桌面快捷方式
def create_shortcut(target, shortcut_path, name):shell = Dispatch('WScript.Shell')shortcut = shell.CreateShortCut(str(shortcut_path))shortcut.Targetpath = targetshortcut.WorkingDirectory = str(Path(target).parent)shortcut.save()desktop_path = Path.home() / 'Desktop'
designer_exe = None
for file in pkg_dir.rglob('Designer.exe'):designer_exe = filebreakif designer_exe:shortcut_name = 'Designer.lnk'create_shortcut(str(designer_exe), desktop_path / shortcut_name, 'Designer')print(f"Shortcut created for Designer.exe on the desktop.")
else:print("Designer.exe not found.")# 3. 在pkgDir中搜索pyuic6.exe并添加到系統環境變量
pyside6_uic_exe_path = None
for file in python_dir.rglob('pyside6-uic.exe'):pyside6_uic_exe_path = file.parentbreak
if pyside6_uic_exe_path:current_path = os.environ['PATH']new_path = f"{current_path};{str(pyside6_uic_exe_path)}"subprocess.run(['setx', 'PATH', new_path, '/M'], check=True)print(f"pyside6-uic.exe directory added to system PATH.")
else:print("pyside6-uic.exe not found.")pyside6_uic_exe = str(pyside6_uic_exe_path) + '\\pyside6-uic.exe'
print('-----------------------------------------------------------------------------')
print(pyside6_uic_exe)
print(designer_exe)
print('-----------------------------------------------------------------------------')