Python3 Numpy 命令不适用于 PyQt5 LineEdit

Python3 Numpy commands not working with PyQt5 LineEdit

我已经编写了通过 tkinter gui 运行的工作代码(所以我知道数学等是有效的)并试图将其转换为 PyQt5 Gui(在 Qt 设计器中导入.ui 文件而不是将代码放在我的 'logic'.py).

'print to shell' 只是在那里,所以我可以看到导致失败的阶段。它在数学函数上失败了。如果我删除一个,它会在下一个失败。第一个失败是:

Line_Theta=np.radians(L_Theta)

我是否需要将从 QLineEdit 读取的内容转换为其他内容(如果需要,如何)?我尝试了以下但没有成功:

Line_Theta=float(np.radians(L_Theta))

L_Theta=float(Line_Theta)

完整代码:

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

BGO_E 和 BGO_N 是东距和北距 (12345.123 & 1234567.123) BinX 和 BinY 均为 6.25(在此示例中) L_Theta 是 direction/compass 轴承。例如。 329.075(我需要小数位以确保准确性)。 输出 BGC_E & BGC_N 也应该是 Eastings & Northings。

我认为这里的问题是您没有正确获取文本。

应该是Line_Theta = np.radians(float(L_Theta.text()))。要访问 QLineEdit 中的文本,您可以使用 text() 属性。这将 return 一个 QString.

更多关于QLineEdit