确定函数/方法是否调用了组合框中的特定项目

时间:2019-09-05 18:44:56

标签: python pyside2

我正在创建一个启动器,我有一个组合框,其中包含两个具有特定功能/方法调用的项,试图检查是否选中了其中一​​项,如果要则调用方法。

我尝试使用组合框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并忽略服务器,即使选择了server也会启动localhost。我希望它是如果选择了localhost,那么它将填充本地的详细信息,但是如果选择了服务器,它将清除IP文本并让用户选择它。

1 个答案:

答案 0 :(得分:1)

如注释中所述,您需要将信号连接到一个方法,然后该方法执行一些操作。似乎您正在尝试改为调用它们,然后使用返回值,但这将不起作用。简化示例:

dbo

我建议您阅读Signals and Slots