我想知道如何从组合框中自动添加或更新qlabel。当我更改QComboBox中的选项时,QLabel应该会更新。
False
答案 0 :(得分:0)
void QComboBox :: currentIndexChanged(const QString&text)
无论何时通过用户交互或以编程方式更改组合框中的currentIndex,都会发送此信号。该项目的文本已通过。
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
convert_options = ["Celcius to Farenheit",
"Celcius to Kelvin",
"Farenheit to Celcius",
"Farenheit to Kelvin",
"Kelvin to Celcius",
"Kelvin to Farenheit"]
class Frame(QWidget):
def __init__(self, convert_options ):
super().__init__()
# uic.loadUi("temperature_converter.ui", self)
self.convert_options = convert_options
self.initUI()
def initUI(self):
self.label_unit = QLabel()
self.comboBox_choices = QComboBox()
self.comboBox_choices.currentIndexChanged[str].connect(self.unit_choice)
self.comboBox_choices.addItems(self.convert_options)
# comboText = self.comboBox_choices.currentText()
# self.unit_choice(comboText)
# self.button_choices()
# This if statement if used needs a button signal
self.grid = QGridLayout()
self.grid.addWidget(self.label_unit, 0, 0)
self.grid.addWidget(self.comboBox_choices, 1, 0)
self.setLayout(self.grid)
def unit_choice(self, comboText):
if comboText == "Celcius to Farenheit":
self.label_unit.setText("Celcius to Farenheit: {}".format(chr(176) + "C"))
elif comboText == "Celcius to Kelvin":
self.label_unit.setText("Celcius to Kelvin: {}".format(chr(176) + "C"))
elif comboText == "Farenheit to Celcius":
self.label_unit.setText("Farenheit to Celcius: {}".format(chr(176) + "F"))
elif comboText == "Farenheit to Kelvin":
self.label_unit.setText("Farenheit to Kelvin: {}".format(chr(176) + "F"))
elif comboText == "Kelvin to Celcius":
self.label_unit.setText("Kelvin to Celcius: {}".format(chr(176) + "K"))
elif comboText == "Kelvin to Farenheit":
self.label_unit.setText("Kelvin to Farenheit: {}".format(chr(176) + "K"))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Frame(convert_options)
w.setWindowTitle(' update a qlabel from the combobox automatically')
w.setWindowIcon(QIcon('im.png'))
w.show()
sys.exit(app.exec())