确定 functions/methods 是否调用了组合框中的特定项目

Determining functions/methods if a specific item in the combobox is called

我正在创建一个启动器,我有一个组合框,其中有两个带有特定 functions/method 调用的项目,试图检查其中一个项目是否被选中,如果是,则调用一个方法。

我尝试使用组合框 currentTextChanged 和 currentIndexChanged 方法,但我没有看到使用该方法的任何新结果。

from PySide2 import QtWidgets
import subprocess
import os
from ui import main, serverMenu

# Initates the basic UI elements
class MyApp(main.Ui_MainWindow, QtWidgets.QMainWindow):
    def __init__(self):
        super(MyApp, self).__init__()
        self.setupUi(self)

        # Sets the combobox item value to a blank item
        self.option.setCurrentIndex(-1)
        # Captures the string from the gamemode combobox
        text = str(self.option.currentText())

            # Checks if local game is set, only allows user to input username and hit the play button
        if(self.option.currentTextChanged == "Local"):
            self.option.setCurrentIndex(1)
            print("I'm in local!")
            self.IP.setText("127.0.0.1")
            self.IP.setReadOnly(True)
            if not self.name.text:
                QtWidgets.QMessageBox.about(self, "IP address", "Hey! \nYou need a name in order to launch the game\n")
            self.playButton.clicked.connect(self.localHost)

            # Checks if server is selected, and blanks out all the details... will also pop up the favorite server details to add servers.
        elif (text == "Server"):
            print("I'm now in server")
            self.IP.setText("")
            if not self.IP.text:
                QtWidgets.QMessageBox.about(self, "IP address", "Hey! \nYou need an IP address in order to launch the game!\n")
            if not self.name.text:
                QtWidgets.QMessageBox.about(self, "IP address", "Hey! \nYou need a name in order to launch the game\n")
            self.playButton.clicked.connect(self.serverHost)
        print("Current text is: " + text)

    # Code to log onto a server that has already been started
    def serverHost(self):
        # Grabs the server text and IP to pass into panda
        username = self.name.text()
        IP_Address = self.IP.text()

        # Sets up enviroment variables needed to launch the game
        os.environ['input'] = '1'
        os.environ['TTS_GAMESERVER'] = IP_Address
        os.environ['TTS_PLAYCOOKIE'] = username

        #os.environ['PANDADIRECTORY'] = 
        subprocess.Popen("C:/Panda3D-1.10.0/python/ppython.exe -m toontown.toonbase.ToontownStart", shell = False)
        self.close()

    # Code to start local host(DEFAULT OPTION)
    def localHost(self):
        backendDir = os.chdir("dev/backend/")
        username = self.name.text()

        # Check to prevent a blank user
        if not username:
            QtWidgets.QMessageBox.about(self, "Name required", "Hey! \nYou need a username before we can start!\n")
            return

        SW_HIDE = 0
        info = subprocess.STARTUPINFO()

        info.dwFlags = subprocess.STARTF_USESHOWWINDOW
        info.wShowWindow = SW_HIDE

        subprocess.Popen(r"start-astron-cluster.bat", startupinfo = info)
        subprocess.Popen(r"start-ai-server.bat", startupinfo = info)
        subprocess.Popen(r"start-uberdog-server.bat", startupinfo = info)

        returnDir = os.chdir("../../")

        os.environ['input'] = '1'
        input = os.environ['input']

        os.environ['TTS_GAMESERVER'] = "127.0.0.1"
        TTS_GAMESERVER = os.environ['TTS_GAMESERVER']

        os.environ['TTS_PLAYCOOKIE'] = username
        #os.environ['PANDADIRECTORY'] = 
        subprocess.Popen("C:\Panda3D-1.10.0\python\ppython.exe -m toontown.toonbase.ToontownStart", shell = False)
        self.close()

if __name__ == "__main__":
    app = QtWidgets.QApplication()
    qt_app = MyApp()
    qt_app.show()
    app.exec_()

如果我只检查 text == Local,即使选择了服务器,它也会调用 localhost 并忽略服务器并启动 localhost。我希望它是如果选择了 localhost,那么它将填写本地的详细信息,但如果选择了服务器,它将清除 IP 文本并让用户选择它。

如评论中所述,您需要将信号连接到一个方法,然后执行某些操作。似乎您正在尝试调用它们,然后使用 return 值,这是行不通的。简化示例:

import sys
from PySide2 import QtWidgets

class TestCombo(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        layout = QtWidgets.QHBoxLayout(self)

        self.combo = QtWidgets.QComboBox()
        self.combo.addItems([None, 'Local', 'Server'])
        self.combo.currentTextChanged.connect(self.announce_change)
        layout.addWidget(self.combo)

    def announce_change(self, txt):
        print(txt)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = TestCombo()
    ex.show()
    sys.exit(app.exec_())

我建议阅读 Signals and Slots