I used pyqt to build a little gui where you can choose a Key
from a QComboBox and the Value
is taken to do a math calculation that usually takes 3 seconds and the result is a short string. I'm updating the calculated short string in the gui in a textbox. This is my code (I removed the math calculation code because it's not relevant), it is working so far:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os
dict1 = {"Key":Value, "Key2":Value2, "Key3":Value3} # Value are int's
class combodemo(QWidget):
def __init__(self, parent = None):
super(combodemo, self).__init__(parent)
layout = QHBoxLayout()
self.cb = QComboBox()
self.cb.addItems([key for key in sorted(dict1.keys())])
self.cb.currentIndexChanged.connect(self.selectionchange)
layout.addWidget(self.cb)
self.setLayout(layout)
self.cb.textbox = QLineEdit(self)
self.cb.textbox.move(100, 200)
self.cb.textbox.resize(150,50)
self.cb.textbox.setAlignment(Qt.AlignCenter)
self.cb.textbox.setText("Initial Text")
def selectionchange(self):
#self.cb.textbox.setText("Calculating...") # THIS IS NOT WORKING
self.cb.currentIndexChanged.connect(self.selectionchange)
# MATH CALCULATION CODE GOES HERE[...]
self.cb.textbox.setText("RESULT OF MATH CALCULATION")
def main():
app = QApplication(sys.argv)
ex = combodemo()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Right now the program freezes for 3 seconds after a Key
is selected (because of the math calculation I am doing). Since my math calculation takes 3 seconds, I would like to update the textbox to "Calculating..."
once a Key
from the QComboBox is selected. So while my math calculation is performed I want the textbox to show "Calculating..."
. Once the calculation is done it should simple rewrite the textbox and show the math result.
In the above code I tried to achieve it in the function def selectionchange(self)
right at the beginning (it's uncommented). But it has no effect. How can I make this work?