How to convert QTextedit's input to int in pyQt4 python -


i new pyqt transitioned tkinter. want ask 2 numbers click button "sum" add both numbers , present output on gui keep getting errors "cant convert str int" or outputs nothing @ all. feel have convert input gotten qtextedit int or float i've tried every possible way know. missing?

code below

import sys pyqt4 import qtcore, qtgui  try:     _fromutf8 = qtcore.qstring.fromutf8 except attributeerror:     def _fromutf8(s):         return s  try:     _encoding = qtgui.qapplication.unicodeutf8     def _translate(context, text, disambig):         return qtgui.qapplication.translate(context, text, disambig, _encoding) except attributeerror:     def _translate(context, text, disambig):         return qtgui.qapplication.translate(context, text, disambig)  class ui_form(qtgui.qwidget):     def __init__(self):         qtgui.qwidget.__init__(self)         self.setupui(self)         qtgui.qapplication.setstyle(qtgui.qstylefactory.create("plastique"))     def setupui(self, form):         form.setobjectname(_fromutf8("form"))         form.resize(400, 300)         self.sum_button = qtgui.qpushbutton(form)         self.sum_button.setgeometry(qtcore.qrect(10, 20, 75, 23))         self.sum_button.setobjectname(_fromutf8("sum_button"))         self.sum_button.clicked.connect(self.action)          self.text1 = qtgui.qtextedit(form)         self.text1.setgeometry(qtcore.qrect(110, 10, 104, 71))         self.text1.setobjectname(_fromutf8("text1"))         self.texxt1 = self.text1.toplaintext()          self.text2 = qtgui.qtextedit(form)         self.text2.setgeometry(qtcore.qrect(110, 140, 104, 71))         self.text2.setobjectname(_fromutf8("text2"))         self.texxt2 = self.text2.toplaintext()          self.clear = qtgui.qpushbutton(form)         self.clear.setgeometry(qtcore.qrect(20, 140, 75, 23))         self.clear.setobjectname(_fromutf8("clear"))          self.retranslateui(form)         qtcore.qobject.connect(self.clear, qtcore.signal(_fromutf8("clicked()")), self.text2.clear)         qtcore.qobject.connect(self.clear, qtcore.signal(_fromutf8("clicked()")), self.text1.clear)         qtcore.qmetaobject.connectslotsbyname(form)      def action(self):         self.tex = self.texxt1         self.ij = self.texxt2         self.l = self.tex + self.ij         qtgui.qlabel(self.l,self)      def retranslateui(self, form):         form.setwindowtitle(_translate("form", "form", none))         self.sum_button.settext(_translate("form", "sum", none))         self.clear.settext(_translate("form", "clear", none))  if __name__ == '__main__':     app = qtgui.qapplication(sys.argv)     ex = ui_form()     ex.show()     sys.exit(app.exec_()) 

you have this:

self.texxt1 = self.text1.toplaintext() # ... self.texxt2 = self.text2.toplaintext() 

and then, in action handler, have this:

def action(self):     self.tex = self.texxt1     self.ij =  self.texxt2     self.l = self.tex + self.ij     qtgui.qlabel(self.l,self) 

first, instead of trying add strings together, need convert values integers. e.g.:

total = int(self.texxt1) + int(self.texxt2) 

you should check exceptions, in case inputs not numeric.

then, have line in handler:

qtgui.qlabel(self.l,self) 

this line creates new label. instead of creating new label, take pre-existing label in gui, , use it's settext method show result. e.g.:

self.result_lbl.settext('{:,}'.format(total)) 

would display result if have qlabel widget named result_lbl.


update

it still isn't outputting result

the source of problem in these lines:

self.texxt1 = self.text1.toplaintext() # ... self.texxt2 = self.text2.toplaintext() 

notice you're trying contents of qtextedits widgets during ui's initialization, before user has had chance enter anything. try compute using empty strings, never work. (btw, think there're better widget choices, qlineedit)

instead, need wait until user has pressed "sum" button work. it's @ point it's reasonable expect user would've entered something.

for example, assuming you've added result label result_lbl suggested, you'd looking @ like:

def action(self):     val1 = int(self.text1.toplaintext())     val2 = int(self.text2.toplaintext())     self.result_lbl.settext(_fromutf8("<b>result:</b> {:,}".format(val1 + val2))) 

to add qlabel, add code snippet setupui method:

self.result_lbl = qtgui.qlabel(form) self.result_lbl.setgeometry(qtcore.qrect(20, 250, 75, 23)) self.result_lbl.setobjectname(_fromutf8("result_lbl")) self.result_lbl.settext(_fromutf8("<b>result:</b>")) 

this tested (using python3) modification code posted , works. if doesn't work you, you'll need re-check code.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -