Python 确定是否调用组合框中的特定项的函数/方法
我正在创建一个启动器,我有一个组合框,其中有两个具有特定函数/方法调用的项,试图检查其中一个项是否已选中,如果已选中,则调用一个方法 我尝试使用组合框currentTextChanged和currentIndexChanged方法,但没有看到使用该方法的任何新结果Python 确定是否调用组合框中的特定项的函数/方法,python,pyside2,Python,Pyside2,我正在创建一个启动器,我有一个组合框,其中有两个具有特定函数/方法调用的项,试图检查其中一个项是否已选中,如果已选中,则调用一个方法 我尝试使用组合框currentTextChanged和currentIndexChanged方法,但没有看到使用该方法的任何新结果 from PySide2 import QtWidgets import subprocess import os from ui import main, serverMenu # Initates the basic UI el
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,即使选择了server。如果选择了localhost,那么它将填充本地的详细信息,但是如果选择了服务器,它将清除IP文本并让用户选择。如注释中所述,您需要将信号连接到一个方法,然后执行一些操作。看起来您正试图调用它们,然后使用返回值,这是行不通的。简化示例:
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_())
我建议阅读currentTextChanged和currentIndexChanged是您需要连接到执行该操作的某个方法的信号。您还没有在此处发布整个应用程序-您在此处发布的代码块是否包含在您正在连接到其中一个信号的方法中?我为该程序发布了全部代码,但之前发布的代码片段是通用GUI。我已经编写了这些方法,但对于如何检查组合框中的索引/文本是否已更改,我有点困惑。localHost和serverHost是在选择组合框索引文本时应该启动/使用的方法。非常感谢。