WillH
WillH

Reputation: 303

Python3 PyQT5 float variable to LineEdit

I am trying to put the float result of a calculation into a LineEdit in PyQT5. I know the LineEdit reference is correct as I can change the text to a set value, eg "Test", but I can't setText to display my calculated variable. All the equations etc are working correctly and print to shell, it is just the output to GUI I am struggling with. I need to do the float conversion as the logic refuses to work if I don't. Do I need to convert it back to something else to get it to work with setText? I have put #Default data examples and answers for each stage in the code.

from PyQt5 import QtWidgets, uic
import sys
import pandas as pd
from pandas import DataFrame
import numpy as np

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
       super(Ui,self).__init__() 
       uic.loadUi('BinGrid.ui',self) 

     
       self.button=self.findChild(QtWidgets.QPushButton,'P6BGCbtn') 
       self.button.clicked.connect(self.P6_to_BGC) 
    
    
       self.show() ## Show the Gui

   def P6_to_BGC(self,MainWindow):  #### Find Bin Grid centre from Bin Grid Origin ####        

       BinX=self.BinXin.text()   ## Default set to 6.25 in Qt Designer
       BinY=self.BinYin.text() ## Default set to 6.25
       L_Theta=self.L_Thetain.text() ## Default set to 329.075
       BGO_E=self.BGO_Ein.text() ## Default set to 123456.123
       BGO_N=self.BGO_Nin.text() ## Default set to 1234567.123

       BinX=float(BinX)
       BinY=float(BinY)
       L_Theta=float(L_Theta)
       Line_Theta=np.radians(L_Theta)
       BGO_E=float(BGO_E)
       BGO_N=float(BGO_N)

       Bin_Theta=np.arctan((0.5*BinX)/(0.5*BinY))                
       Bin_Hyp=((0.5*BinX)**2+(0.5*BinY)**2)**0.5
       BGC_E=round(BGO_E+Bin_Hyp*np.sin(Bin_Theta+Line_Theta),3) ## Output is 123457.075
       BGC_N=round(BGO_N+Bin_Hyp*np.cos(Bin_Theta+Line_Theta),3) ## Output is 1234571.287       
        


       self.BGC_Ein.setText('BGC_E') ## This works to set the box to 'BGC_E'
       self.BGC_Ein.repaint() ## Corrects for know 'non-display until focus' bug
       self.BGC_Nin.setText(BGC_N.text()) ## This doesn't work to set the text to the value of my variable
       self.BGC_Nin.repaint()

app=QtWidgets.QApplication(sys.argv) 
window=Ui() 
app.exec_() 

Upvotes: 0

Views: 1106

Answers (1)

titusjan
titusjan

Reputation: 5546

QLineEdit.setText() expects a string parameter, so you have to convert your float to string first. E.g.:

self.BGC_Nin.setText(str(BGC_N))

Also, you could use string formatting to round your numbers, so that you don't have to call round() yourself. E.g.:

BGC_N = BGO_N+Bin_Hyp*np.cos(Bin_Theta+Line_Theta) ## Output is 1234571.287       
self.BGC_Ein.setText("{:.3f}".format(BGC_N)) 

Upvotes: 1

Related Questions