0

I'm creating a launcher, and I have a combobox that has two items with specific functions/method calls, trying to have checks if one of the items is checked and to call a method if so.

I tried using the combobox currentTextChanged and currentIndexChanged methods, but I don't see any new results from using the method.

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_()

If I just check for text == Local, it'll call localhost and ignores server and launches localhost even if server is selected. I want it to be if localhost is selected, then it'll fill the details for local, but if the server is selected, it'll clear the the IP text and let the user pick it.

zenith110
  • 11
  • 4
  • 1
    `currentTextChanged` and `currentIndexChanged` are signals that you need to connect to some method that will perform that action. You haven't posted the entire application here - is the code block that you posted here contained within a method that you're connecting to one of these signals? – dan_g Sep 05 '19 at 18:56
  • I put the whole code for the program, but the snippet I had posted before is the general GUI. I have the methods already written, but I'm just a bit confused on how to check if the index/text has been changed in the combobox. localHost and serverHost are the methods that are supposed to be launched/used when the combobox index texts for those are selected. Thank you. – zenith110 Sep 05 '19 at 19:28

1 Answers1

1

As mentioned in the comment, you need to connect the signals to a method that then performs some action. It seems like you're trying to call them instead and then use the return value, which won't work. Simplified example:

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_())

I would suggest reading up on Signals and Slots

dan_g
  • 2,712
  • 5
  • 25
  • 44