Python Forum
python interactive output with Qt4
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python interactive output with Qt4
#1
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_())
Reply
#2
Please put the code in Python code tags. You can find help here.
Reply
#3
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_())
Reply
#4
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.
Reply
#5
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.
Reply
#6
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+b
In 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
Reply
#7
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.
Reply
#8
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.
Reply
#9
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
  • solve a problem highlighted by an error message.
    confirmed that my theory was correct and only the practice let me down.
    I have gone back to my python tutorial and found the instruction in the string manipulation list and I can see other interesting code which I can try.
    I can now see how to improve my Qy4 Gui to make it even more user friendly and extend it to plot a graph of the formula.
    and last but not least saved my marriage of 56 years from an illness known as computer programitus which has lasted this time for 1 week. Many thanks.

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  "Interactive" figure in a GUI puje 2 4,534 Jun-24-2020, 06:55 PM
Last Post: puje

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020