gpt4 book ai didi

python - 如何在 if 循环中使用 QCombobox 选择?

转载 作者:太空宇宙 更新时间:2023-11-03 17:28:56 26 4
gpt4 key购买 nike

我是 GUI 新手,我正在尝试让用户在组合框中的选择改变我的程序的结果。

这是我的组合框:

self.popupItems1 = ("Option 1","Option 2")
self.popup1 = QtGui.QComboBox(self)
self.popup1.addItems(self.popupItems1)
self.popup1.setCurrentIndex(self.popupItems1.index("Option 1"))
self.popup1.move(10, 220)

我做了很多研究,但我似乎无法弄清楚这一点,我猜我应该使用这样的东西?

if self.popupItems1 == 'Option 1':
do_something()
else:
do_something_else()

预先感谢您的帮助!

最佳答案

要获取 QComboBox 中当前选定的文本,请使用方法 currentText(),如果要获取索引,请使用方法 currentIndex()

例如,如果您的 QComboBox 被 self.popup1 引用,那么要获取所选文本,请使用:

text = self.popup1.currentText()

您还可以使用其他方法获取索引。

您正在寻找的可能是这样的:

if self.popup1.currentIndex() == 0 : # The first option
do_something()
else : # Any other option
do_something_else()

欲了解更多信息,请查看 the documentation of QComboBox .

关于python - 如何在 if 循环中使用 QCombobox 选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32178506/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com