[PyQt] Use QTextEdit() clear slot

Simon Stockes simonstockes at gmail.com
Mon Jan 21 20:13:50 GMT 2008


Hi All,

It seems that  "cleanButton connect" stop to a new expression  
evaluation in the following code ?

1/ If you comment this connection the program works again !!

2/ If you "reset" with the "cleanButton" something you wrote in the  
QTextEdit, it cleans it !!



Any track ?


Simon



CODE
=====


from __future__ import division

import sys
from math import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class Ui_MainWindow(QDialog):

     def __init__(self, parent=None):
         super(Ui_MainWindow, self).__init__(parent)
         self.browser = QTextEdit()
         self.lineedit = QLineEdit("Enter an expression anf press  
Enter")
         self.lineedit.selectAll()
         self.cleanButton = QPushButton("Reset")
         layout = QVBoxLayout()
         layout.addWidget(self.browser)
         layout.addWidget(self.lineedit)
         layout.addWidget(self.cleanButton)
         self.setLayout(layout)
         self.lineedit.setFocus()
         self.setWindowTitle("Calculator")

#???????????????????????????????????????????

         self.connect(self.cleanButton, SIGNAL("clicked()"),  
self.clearForm)

  #??????????????????????????????????????????

     def clearForm(self):
         self.browser.clear()


class App(object):
     """ Run Calculator's GUI, main controller class (MAIN CONTROLER)"""

     def __init__(self):
         """ Creation of Calculator's GUI and connexion with user  
events """

         # Creation of the Calculator's GUI:
         self.mainwindow = Ui_MainWindow()

         self.mainwindow.connect(self.mainwindow.lineedit, SIGNAL 
("returnPressed()"),  self.updateUi)

     def updateUi(self):
         try:
             text = unicode(self.mainwindow.lineedit.text())
             self.mainwindow.browser.append("%s = <b>%s<b><br>" % 
(text, eval(text)) )
             self.mainwindow.lineedit.selectAll()
         except:
             self.mainwindow.browser.append(('<font color="red">%s is  
wrong<font><br>' %text))
             self.mainwindow.lineedit.selectAll()


if __name__ == "__main__":
     app = QApplication(sys.argv)

     # init GUI
     calc = App()

     # display of GUI
     calc.mainwindow.show()

     app.exec_()







More information about the PyQt mailing list