m0_66512479 2022-01-19 10:25 采纳率: 40%
浏览 23
已结题

使用qt designer设计了一个求视场角的窗口,但一直不知道怎么在lineedit中获取数字去计算,而且代码中应该也有一些别的问题,求解决

问题遇到的现象和发生背景

主代码

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QDialog
from jsscjck import Ui_Dialog
import math

def comput_degree(a_f, f_f):
    degree = 2 * math.atan(a_f / f_f / 2)
    return degree

class Using_test(QDialog, Ui_Dialog):
    def __init__(self, *args, **kwargs):
        super(Using_test, self).__init__(*args, **kwargs)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.calcu())

    def calcu(self):
        a = self.lineEdit.text().todouble()
        b = self.lineEdit_2.text().todouble()
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/627209855246151.png "#left")

        f = self.lineEdit_3.text().todouble()
        alpha = math.degrees(comput_degree(a, f))
        beta = math.degrees(comput_degree(b, f))
        print("alpha = %.4f" % alpha)
        print("beta = %.4f" % beta)
        self.lineEdit_4.setText(alpha)
        self.lineEdit_5.setText(beta)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Using_test()
    win.show()
    sys.exit(app.exec_())
窗体代码
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'D:\python\python_work\计算视场角.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!




from PyQt5 import QtCore, QtGui, QtWidgets



class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(610, 345)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(120, 100, 31, 31))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(120, 140, 31, 31))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(116, 180, 31, 31))
        self.label_3.setObjectName("label_3")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(150, 100, 113, 31))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
        self.lineEdit_2.setGeometry(QtCore.QRect(150, 140, 113, 31))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_3 = QtWidgets.QLineEdit(Dialog)
        self.lineEdit_3.setGeometry(QtCore.QRect(150, 180, 113, 31))
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(240, 240, 92, 28))
        self.pushButton.setObjectName("pushButton")
        self.label_4 = QtWidgets.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(330, 110, 72, 31))
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(Dialog)
        self.label_5.setGeometry(QtCore.QRect(330, 150, 72, 31))
        self.label_5.setObjectName("label_5")
        self.lineEdit_4 = QtWidgets.QLineEdit(Dialog)
        self.lineEdit_4.setGeometry(QtCore.QRect(400, 110, 113, 31))
        self.lineEdit_4.setObjectName("lineEdit_4")
        self.lineEdit_5 = QtWidgets.QLineEdit(Dialog)
        self.lineEdit_5.setGeometry(QtCore.QRect(400, 150, 113, 31))
        self.lineEdit_5.setObjectName("lineEdit_5")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "a:"))
        self.label_2.setText(_translate("Dialog", "b:"))
        self.label_3.setText(_translate("Dialog", "f:"))
        self.pushButton.setText(_translate("Dialog", "计算"))
        self.label_4.setText(_translate("Dialog", "水平角α:"))
        self.label_5.setText(_translate("Dialog", "垂直角β:"))

窗体见附图

我想在左侧的三个lineedit里面输入数字,然后通过函数计算输出在右面的两个里面。



  • 写回答

1条回答 默认 最新

  • llc的足迹 2022-01-19 11:07
    关注

    connect链接的函数不需要括号self.pushButton.clicked.connect(self.calcu),字符串转浮点使用float

    class Using_test(QDialog, Ui_Dialog):
        def __init__(self, *args, **kwargs):
            super(Using_test, self).__init__(*args, **kwargs)
            self.setupUi(self)
            self.pushButton.clicked.connect(self.calcu)
    
        def calcu(self):
            a = self.lineEdit.text()
            b = self.lineEdit_2.text()
            f = self.lineEdit_3.text()
            alpha = math.degrees(comput_degree(float(a), float(f)))
            beta = math.degrees(comput_degree(float(a), float(f)))
            print("alpha = %.4f" % alpha)
            print("beta = %.4f" % beta)
            self.lineEdit_4.setText(str(alpha))
            self.lineEdit_5.setText(str(beta))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月28日
  • 已采纳回答 1月20日
  • 创建了问题 1月19日

悬赏问题

  • ¥15 FOC simulink
  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?