Search code examples
python-3.xnumpyuser-interfacepyqt5qlineedit

Python3 Numpy commands not working with PyQt5 LineEdit


I have written working code operating via a tkinter gui (so I know the maths etc works) and am trying to convert it to a PyQt5 Gui (importing in the Qt designer .ui file rather than having the code in my 'logic'.py).

The 'print to shell' are only there so I can see the stage causing the failure. It fails at the maths functions. If I remove one, it fails at the next. First fail is:

Line_Theta=np.radians(L_Theta)

Do I need to convert what is being read from the QLineEdit into something else (if so, how)? I have tried the following with no success:

Line_Theta=float(np.radians(L_Theta))

and

L_Theta=float(Line_Theta)

Full 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__() ## Call the inherited classes __init__ method
       uic.loadUi('BinGrid.ui',self) ## Load the .ui file

     
       self.button=self.findChild(QtWidgets.QPushButton,'P6BGCbtn') ## Find the button
       self.button.clicked.connect(self.P6_to_BGC) ## Remember to pass the defintion/method
    
       self.show() ## Show the Gui

   def P6_to_BGC(self,MainWindow):  #### Find Bin Grid centre from Bin Grid Origin ####
      BinX=self.findChild(QtWidgets.QLineEdit, 'BinXin')
      BinY=self.findChild(QtWidgets.QLineEdit, 'BinYin')
      L_Theta=self.findChild(QtWidgets.QLineEdit, 'L_Thetain')
      BGO_E=self.findChild(QtWidgets.QLineEdit, 'BGO_Ein')
      BGO_N=self.findChild(QtWidgets.QLineEdit, 'BGO_Nin')

      Line_Theta=np.radians(L_Theta)

      print(BinX.text()) ##Print only here to check failure stage
      print(BinY.text())
      print(L_Theta.text())
      print(Line_Theta.text())
      print(BGO_E.text())
      print(BGO_N.text())
    
      Bin_Theta=np.arctan((0.5*BinX)/(0.5*BinY))

      print(Bin_Theta.text())
    
      Bin_Hyp=((0.5*BinX)**2+(0.5*BinY)**2)**0.5

      print(Bin_Hyp.text())
      BGC_E=round(BGO_E+Bin_Hyp*np.sin(Bin_Theta+Line_Theta),3)
      BGC_N=round(BGO_E+Bin_Hyp*np.cos(Bin_Theta+Line_Theta),3)        
    
      print(BGC_E.text())
      print(BGC_N.text())

      self.BGC_Ein.setText(BGC_E.text())
      self.BGC_Nin.setText(BGC_E.text())

app=QtWidgets.QApplication(sys.argv) ## Create an instance of QtWidgets.QApplication
window=Ui() ##Create an instance of our class
app.exec_() ##Start the application

The BGO_E and BGO_N are Easting & Northing (12345.123 & 1234567.123) BinX & BinY both 6.25 (in this example) L_Theta is a direction/compass bearing. eg. 329.075 (I need the decimal places for accuracy). The Outputs BGC_E & BGC_N should also be Eastings & Northings.


Solution

  • I think the problem here is you aren't obtaining the text properly.

    It should be Line_Theta = np.radians(float(L_Theta.text())). To access the text inside the QLineEdit you can use the text() property. This will return a QString.

    More on QLineEdit