如何在不立即激活另一个功能的情况下从QComboBox存储和传递值?

时间:2019-02-04 21:26:37

标签: python-3.x pyqt5 qcombobox

如何仅在用户按下按钮“策略1”之后而不是在他/她刚刚选择货币时才使应用程序打印“您单击了货币XXX的按钮1”?

这是我到目前为止所做的,但是在用户从QComboBox中选择货币后,它会立即打印“您已点击...”

from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt

app = QApplication([])

def button1_clicked(curr_pair):
    alert = QMessageBox()
    alert.setText('You clicked the button1 for currency {}'.format(curr_pair))
    alert.exec_()

button1 = QPushButton('Strategy1')
button1.setToolTip('Click to run strategy 1')
button1.show()
button1.clicked.connect(button1_clicked)

combo = QComboBox()
combo.addItems(['', 'eur', 'usd', 'gbp'])
combo.show()
combo.activated[str].connect(button1_clicked)

window = QWidget()
layout = QVBoxLayout()

title1 = QLabel("Choose currency") 
title1.setAlignment(Qt.AlignCenter) 
title2 = QLabel("Push the button") 
title2.setAlignment(Qt.AlignCenter) 

layout.addWidget(title1)
layout.addWidget(combo)
layout.addWidget(title2)
layout.addWidget(button1)

window.setLayout(layout)
window.show()

app.exec_()

1 个答案:

答案 0 :(得分:0)

尝试一下:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt

app = QApplication([])

def button1_clicked(curr_pair):
    alert = QMessageBox()
    if curr_pair:
        alert.setText('You clicked the button1 for currency: `{}`'.format(curr_pair))
    else:
        alert.setText('You have pressed the button1 no currency is selected !!!')
    alert.exec_()

currencyName = QLabel("")                                              # <--- +++

button1 = QPushButton('Strategy1')
button1.setToolTip('Click to run strategy 1') 
button1.show()

#button1.clicked.connect(button1_clicked)
button1.clicked.connect(lambda : button1_clicked(currencyName.text())) # <--- +++

combo = QComboBox()
combo.addItems(['', 'eur', 'usd', 'gbp'])
combo.show()

#combo.activated[str].connect(button1_clicked)
combo.activated[str].connect(currencyName.setText)                      # <--- +++

window = QWidget()
layout = QVBoxLayout()

title1 = QLabel("Choose currency")      
title1.setAlignment(Qt.AlignCenter) 
title2 = QLabel("Push the button")      
title2.setAlignment(Qt.AlignCenter) 

layout.addWidget(title1)
layout.addWidget(combo)
layout.addWidget(title2)
layout.addWidget(button1)

window.setLayout(layout)
window.show()

app.exec_()

enter image description here