python interactive output with Qt4 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: python interactive output with Qt4 (/thread-9030.html) |
python interactive output with Qt4 - eegrek39 - Mar-18-2018 I am trying to print a fibonacci sequence in a QTextEdit box in Qt4 and I am only getting the last number. How do I print the whole sequence my code is class MyApp(QtGui.QMainWindow, Ui_MainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) Ui_MainWindow.__init__(self) self.setupUi(self) self.runButton.clicked.connect(self.calculate) ## Fibonacci sequence code def calculate(self): a, b = 0, 1 while b < 10: t = str(b) output = t self.outputText.setText(output) a, b = b, a+b if __name__ == "__main__": app = QtGui.QApplication(sys.argv) window = MyApp() window.show() sys.exit(app.exec_()) RE: python interactive output with Qt4 - j.crater - Mar-18-2018 Please put the code in Python code tags. You can find help here. RE: python interactive output with Qt4 - eegrek39 - Mar-18-2018 class MyApp(QtGui.QMainWindow, Ui_MainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) Ui_MainWindow.__init__(self) self.setupUi(self) self.runButton.clicked.connect(self.calculate) ## Fibonacci sequence code def calculate(self): a, b = 0, 1 while b < 10: t = str(b) output = t self.outputText.setText(output) a, b = b, a+b if __name__ == "__main__": app = QtGui.QApplication(sys.argv) window = MyApp() window.show() sys.exit(app.exec_()) RE: python interactive output with Qt4 - j.crater - Mar-18-2018 Thanks for putting code in code tags. But please edit the code indentation. It is crucial in Python and also without it code is hardly readable. RE: python interactive output with Qt4 - eegrek39 - Mar-18-2018 import sys from PyQt4 import QtCore, QtGui, uic qtCreatorFile = "codeoutput.ui" # Enter file here. Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile) class MyApp(QtGui.QMainWindow, Ui_MainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) Ui_MainWindow.__init__(self) self.setupUi(self) self.runButton.clicked.connect(self.calculate) ## Fibonacci sequence code def calculate(self): a, b = 0, 1 while b < 10: t = str(b) output = t self.outputText.setText(output) a, b = b, a+b if __name__ == "__main__": app = QtGui.QApplication(sys.argv) window = MyApp() window.show() sys.exit(app.exec_()) I would like to add a further explanation I have created a window using QT4 Designer which includes a TextEdit widget and a button called runButton which when pressed runs the python code. Everything works o.k. except when I look at the Window, only the last figure of the sequence is shown. I want to duplicate the complete sequence which would be normally shown in the python shell. I have tried to save the sequence as a list but it does not like to import a list using the instruction 'self.outputText.setText(output)'. The problem could be with the text box in qt4 where perhaps I have not got the attributes correct. It might be in the python code were I am not creating a correct list. RE: python interactive output with Qt4 - j.crater - Mar-18-2018 I did not run your code (also don't have Qt installed), so please be advised I am taking a blind shot at answering. In this part of code: while b < 10: t = str(b) output = t self.outputText.setText(output) a, b = b, a+bIn each loop iteration, output = t overrides the previous value of output, and setText(output) overrides previously printed text with new value of output. It probably happens so fast that you end up seeing only the last value.There are several possible solutions. For example, you could concatenate the string in each iteration and print the new "extended" string (sequence of numbers). But I would do this by storing calculated values in a list (Python's version of array) and print the list in the end. def calculate(self): a, b = 0, 1 values = [] # assign an empty list to be used to store values while b < 10: values.append(b) # adds an item to list, at this point you don't need to convert int to str a, b = b, a+b self.outputText.setText(' '.join(values)) # print the list as string, with single whitespace as delimiter between items RE: python interactive output with Qt4 - eegrek39 - Mar-18-2018 This is magnificent - thank you very much. It was what I was trying to do but not being very successful. I did however have to make t= str(b) for it to work. It does and I can't thank you enough. RE: python interactive output with Qt4 - j.crater - Mar-18-2018 I made an embarrassing mistake, the documentation clearly states that join method joins strings which are contained in an iterable. So sure enough you still had to cast the variable to string. Well, lesson learned, you can always rely on documentation :D And I am glad you got it working. RE: python interactive output with Qt4 - eegrek39 - Mar-18-2018 Well you see you have more than just helped to solve what must be to you a little problem. You have given me the confidence to
|