当前位置:   article > 正文

PySide6+VSCode Python可视化环境搭建

PySide6+VSCode Python可视化环境搭建
pip install pyside6

下载本期源码
vscode装一个PYQT Integration插件,设置好两个路径(下面有个脚本用于获取路径)

everything的童鞋注意了:工具/选项/索引/强制重建

重启vscode可以看到,右击.ui文件时出现可以操作的菜单

我们New一个Form默认生成一个.ui文件,然后点击编译,就会出现我们需要的Ui_untitled.py,这就是编译出来的文件。在test.py中输入以下代码F5运行,对话框就出现了

  1. import sys
  2. from Ui_untitled import Ui_Dialog
  3. from PySide6.QtWidgets import QApplication, QDialog # 假设您使用的是 PyQt5
  4. class MyDialog(QDialog):
  5. def __init__(self):
  6. super(MyDialog, self).__init__()
  7. self.ui = Ui_Dialog() # 假设 Ui_Dialog 是您的 UI 文件生成的类
  8. self.ui.setupUi(self) # 将当前对话框实例传递给 setupUi 方法
  9. if __name__ == "__main__":
  10. app = QApplication([])
  11. win = MyDialog()
  12. win.show()
  13. app.exec_()

最后附上一段获取exe文件路径的脚本,方便路径的复制粘贴:(注意不要重复运行,里面有添加环境变量操作,重复运行会添加重复的环境变量)

  1. import os
  2. import sys
  3. import subprocess
  4. from pathlib import Path
  5. from win32com.client import Dispatch #要安装依赖:python -m pip install pypiwin32
  6. # 1. 搜索Python目录并设置pkgDir变量
  7. def find_python_directory():
  8. for root, dirs, files in os.walk('C:/Users/Administrator/AppData/Local/Programs'):
  9. if 'python.exe' in files:
  10. return Path(root)
  11. return None
  12. python_dir = find_python_directory()
  13. if python_dir is None:
  14. print("Python not found.")
  15. sys.exit(1)
  16. pkg_dir = python_dir / 'Lib' / 'site-packages'
  17. print(f"Found Python at: {python_dir}")
  18. print(f"Package directory: {pkg_dir}")
  19. # 2. 在pkgDir中搜索Designer.exe并创建桌面快捷方式
  20. def create_shortcut(target, shortcut_path, name):
  21. shell = Dispatch('WScript.Shell')
  22. shortcut = shell.CreateShortCut(str(shortcut_path))
  23. shortcut.Targetpath = target
  24. shortcut.WorkingDirectory = str(Path(target).parent)
  25. shortcut.save()
  26. desktop_path = Path.home() / 'Desktop'
  27. designer_exe = None
  28. for file in pkg_dir.rglob('Designer.exe'):
  29. designer_exe = file
  30. break
  31. if designer_exe:
  32. shortcut_name = 'Designer.lnk'
  33. create_shortcut(str(designer_exe), desktop_path / shortcut_name, 'Designer')
  34. print(f"Shortcut created for Designer.exe on the desktop.")
  35. else:
  36. print("Designer.exe not found.")
  37. # 3. 在pkgDir中搜索pyuic6.exe并添加到系统环境变量
  38. pyside6_uic_exe_path = None
  39. for file in python_dir.rglob('pyside6-uic.exe'):
  40. pyside6_uic_exe_path = file.parent
  41. break
  42. if pyside6_uic_exe_path:
  43. current_path = os.environ['PATH']
  44. new_path = f"{current_path};{str(pyside6_uic_exe_path)}"
  45. subprocess.run(['setx', 'PATH', new_path, '/M'], check=True)
  46. print(f"pyside6-uic.exe directory added to system PATH.")
  47. else:
  48. print("pyside6-uic.exe not found.")
  49. pyside6_uic_exe = str(pyside6_uic_exe_path) + '\\pyside6-uic.exe'
  50. print('-----------------------------------------------------------------------------')
  51. print(pyside6_uic_exe)
  52. print(designer_exe)
  53. print('-----------------------------------------------------------------------------')

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/195840
推荐阅读
相关标签
  

闽ICP备14008679号