重量计算器

2024-05-29 05:55:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在qt designer中创建一个重量计算器,我想在上面添加一些功能,但我不能,请您检查一下

这是我的主页ui代码: 我的ui文件:google link

import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi


class MainPage(QDialog):
    def __init__(self):
        super(MainPage, self).__init__()
        loadUi('Homepage.ui', self)
        self.pushButton.clicked.connect(self.retrieveText)

    def takeText(self):
        x = self.plainTextEdit_2.currentText()
        self.textEdit.setText(x)


    def retrieveText(self):

        number=self.plainTextEdit.toPlainText()
        number=float(number) * my below fuction
        self.textEdit.setText(str(number))


app=QApplication(sys.argv)
widget=MainPage()
widget.show()
sys.exit(app.exec_())

我想在上面添加一些功能:

gender=input("If Man, please type M or if women type W: ")
heigh=int(input("Whats your heigh: "))
if(gender=="M"):
    r=int(((heigh*4/2.54)-128)*0.453)
    print("Your ideal weight: " + str(r))

elif(gender=="W"):
    r=int(((heigh*3.5/2.54)-108)*0.453)
    print("Your ideal weight: "+str(r))

用户在第一个文本框中键入性别(M或W),在第二个文本框中键入高度,当单击结果按钮时,我们可以在最后一个框中看到result


Tags: fromimportself功能uinumberdefsys
1条回答
网友
1楼 · 发布于 2024-05-29 05:55:43

是的,我发现它很简单:)

import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi

class MainPage(QDialog):
    def __init__(self):
        super(MainPage, self).__init__()
        loadUi('HomePage.ui', self)
        self.pushButton.clicked.connect(self.takeText)
        self.fillCombobox()

    def fillCombobox(self):
        gender = ["M", "W"]
        self.comboBox.addItems(gender)

    def takeText(self):
        gender = self.comboBox.currentText()
        heigh = int(self.plainTextEdit.toPlainText())
        if gender == "M":
            r = int(((heigh * 4 / 2.54) - 128) * 0.453)
        elif gender == "W":
            r = int(((heigh * 3.5 / 2.54) - 108) * 0.453)

        self.textEdit.setText(str(r))


if __name__ == "__main__":
    app=QApplication(sys.argv)
    widget=MainPage()
    widget.show()
    sys.exit(app.exec_())

相关问题 更多 >

    热门问题