【问题标题】:PySide2 app that doesn't launch via VSCODE不通过 VSCODE 启动的 PySide2 应用程序
【发布时间】:2021-01-19 08:23:38
【问题描述】:

我尝试编写一个非常简单的货币转换器应用程序,因此脚本应该打开一个带有货币转换器的窗口。

但是当我在我的 Mac 上通过 VS Code 在 Python 终端中运行该文件时,它会打开一个没有响应的 Python 启动器窗口。 当我在 IDLE 中打开此脚本并运行它时,它可以完美运行(在我的 Mac 上)。

当我在我的 Windows10 笔记本电脑上通过 VS Code 在 Python 终端中运行相同的文件时,它运行良好。

我使用 Python 3.9 和相同的库(PySide2 和 currencyConverter 都安装在两个设备上)

from PySide2 import QtWidgets, QtGui, QtCore
import currency_converter


class App(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.c = currency_converter.CurrencyConverter()
        self.setWindowTitle("Convertisseur de devises")
        self.setup_ui()
        self.setup_connections()
        self.set_default_values()
        self.setup_css()
        self.resize(500, 50)

    def setup_ui(self):
        self.layout = QtWidgets.QHBoxLayout(self)
        self.cbb_devisesFrom = QtWidgets.QComboBox()
        self.le_montant = QtWidgets.QSpinBox()
        self.cbb_devisesTo = QtWidgets.QComboBox()
        self.le_montantConverti = QtWidgets.QSpinBox()
        self.btn_inverser = QtWidgets.QPushButton("Inverser devises")

        self.layout.addWidget(self.cbb_devisesFrom)
        self.layout.addWidget(self.le_montant)
        self.layout.addWidget(self.cbb_devisesTo)
        self.layout.addWidget(self.le_montantConverti)
        self.layout.addWidget(self.btn_inverser)

    def setup_connections(self):
        self.cbb_devisesFrom.activated.connect(self.compute)
        self.cbb_devisesTo.activated.connect(self.compute)
        self.le_montant.valueChanged.connect(self.compute)
        self.btn_inverser.clicked.connect(self.inverser_devises)

    def setup_css(self):
        self.setStyleSheet("""
        background-color: rgb(30, 30, 30);
        color: rgb(240, 240, 240);
        border: none;
        """)
        style = """
        QComboBox::down-arrow {
            image: none;
            border-width: 0px;
        }
        QComboBox::drop-down {
            border-width: 0px;
        } 
        """
        self.cbb_devisesFrom.setStyleSheet(style)
        self.cbb_devisesTo.setStyleSheet(style)

    def set_default_values(self):
        self.cbb_devisesFrom.addItems(sorted(list(self.c.currencies)))
        self.cbb_devisesTo.addItems(sorted(list(self.c.currencies)))
        self.cbb_devisesFrom.setCurrentText("EUR")
        self.cbb_devisesTo.setCurrentText("EUR")
        self.le_montant.setValue(100)
        self.le_montantConverti.setValue(100)
        self.le_montant.setRange(1, 1000000)
        self.le_montantConverti.setRange(1, 1000000)

    def compute(self):
        montant = self.le_montant.value()
        deviseFrom = self.cbb_devisesFrom.currentText()
        deviseTo = self.cbb_devisesTo.currentText()

        try:
            resultat = self.c.convert(montant, deviseFrom, deviseTo)
        except currency_converter.currency_converter.RateNotFoundError:
            print("Rate not found")
        else:
            self.le_montantConverti.setValue(resultat)

    def inverser_devises(self):
        devise_from = self.cbb_devisesFrom.currentText()
        devise_to = self.cbb_devisesTo.currentText()

        self.cbb_devisesFrom.setCurrentText(devise_to)
        self.cbb_devisesTo.setCurrentText(devise_from)
        self.compute()


app = QtWidgets.QApplication([])
win = App()
win.show()
app.exec_()

【问题讨论】:

  • 你试过在终端上运行它吗?
  • 您好,感谢您的帮助!是的,我试过并得到了这个错误:achillederibreux@MacBook-Pro-de-Achille app % python App.py Traceback(最近一次调用最后一次):文件“App.py”,第 1 行,在 from PySide2 import QtWidgets ImportError : 没有名为 PySide2 achillederibreux@MacBook-Pro-de-Achille app 的模块 % pip3.9 list Package Version ------ -------- currency.converter 0.5.5 CurrencyConverter 0.14.4 pip 20.3.3 PySide2 5.15.2 setuptools 49.2.1 shiboken2 5.15.2
  • 好像你还没有安装那个模块,试试运行pip install PySide2
  • 嗨!谢谢你帮助我!它告诉我它已经安装了..“要求已经满足:PySide2 in...”并且它适用于 Idle
  • 我怀疑你安装了多个版本的python,尝试用python3和python运行它

标签: python python-3.x visual-studio-code pyside2


【解决方案1】:

请在VS Code终端使用命令“pip show PySide2”(或“pip3 show PySide2”)检查模块“PySide2”的安装位置是否与左下角显示的Python环境一致VS 代码:

如果结果不一致,请使用快捷键Ctrl+Shift+`打开一个新的VS Code终端,它会自动进入选择的环境。

【讨论】:

  • 嗨!谢谢你帮助我!我找到了解决方案,它是 PySide2 和 MacOs Big sur 之间的一个错误,我使用了模块 mathplotlib,它现在运行良好!
猜你喜欢
  • 2018-12-24
  • 1970-01-01
  • 2019-09-11
  • 2012-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
相关资源
最近更新 更多