如何仅在用户按下按钮“策略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_()
答案 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_()