简体   繁体   English

Python3 PyQT5 浮点变量到 LineEdit

[英]Python3 PyQT5 float variable to LineEdit

I am trying to put the float result of a calculation into a LineEdit in PyQT5.我正在尝试将计算的浮点结果放入 PyQT5 中的 LineEdit 中。 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.我知道 LineEdit 引用是正确的,因为我可以将文本更改为设定值,例如“测试”,但我无法通过 setText 显示我的计算变量。 All the equations etc are working correctly and print to shell, it is just the output to GUI I am struggling with.所有方程等都正常工作并打印到 shell,它只是 output 到我正在努力的 GUI。 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?我是否需要将其转换回其他内容以使其与 setText 一起使用? I have put #Default data examples and answers for each stage in the code.我已经为代码中的每个阶段放置了#Default 数据示例和答案。

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_() 

QLineEdit.setText() expects a string parameter, so you have to convert your float to string first. QLineEdit.setText()需要一个字符串参数,因此您必须先将浮点数转换为字符串。 Eg:例如:

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.此外,您可以使用字符串格式对数字进行四舍五入,这样您就不必自己调用round() Eg:例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM