PyQt 使用一键将温度转换为摄氏度和华氏度
PyQt convert temperature to Celsius and Fahrenheit using one button
我刚开始使用 PyQt5 和 so far I'm able to convert celsius to Fahrenheit but I'm a little confused on how to convert to celsius when I type a value in the Fahrenheit lineEdit box, example。如何检查任一框中是否有值并使用单个按钮将其转换?
temperature converter
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(299, 140)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 40, 121, 21))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(160, 40, 121, 21))
self.lineEdit_2.setObjectName("lineEdit_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 70, 75, 23))
self.pushButton.setObjectName("pushButton")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(160, 20, 61, 16))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(10, 20, 61, 16))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(140, 40, 21, 20))
self.label_4.setObjectName("label_4")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 299, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.Celsius_Line)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Convert"))
self.label_2.setText(_translate("MainWindow", "Fahrenheit"))
self.label_3.setText(_translate("MainWindow", "Celsius"))
self.label_4.setText(_translate("MainWindow", "="))
def Celsius_Line(self):
c = str(float(self.lineEdit.text()) * (9/5) + 32 )
self.lineEdit_2.setText(c)
def Fahrenheit_Line(self):
f = str(float(self.lineEdit_2.text()) * (9/5) - 32)
self.lineEdit.setText(f)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我不太明白问题出在哪里。
但据我了解,您想检查 "lineEdit" 部分是否为空。
if lineEdit.text() != '':
如果这样做,如果 "lineEdit" 为空,将返回 "True",您可以相应地更正代码。
修改代码,如果摄氏度为空,则从华氏度转换。否则 cel 到华氏度
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(299, 140)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 40, 121, 21))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(160, 40, 121, 21))
self.lineEdit_2.setObjectName("lineEdit_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 70, 75, 23))
self.pushButton.setObjectName("pushButton")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(160, 20, 61, 16))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(10, 20, 61, 16))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(140, 40, 21, 20))
self.label_4.setObjectName("label_4")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 299, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.Converteither) #Mod here
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Convert"))
self.label_2.setText(_translate("MainWindow", "Fahrenheit"))
self.label_3.setText(_translate("MainWindow", "Celsius"))
self.label_4.setText(_translate("MainWindow", "="))
def Celsius_Line(self):
c = str(float(self.lineEdit.text()) * (9/5) + 32 )
self.lineEdit_2.setText(c)
def Fahrenheit_Line(self):
f = str(float(self.lineEdit_2.text()) * (9/5) - 32)
self.lineEdit.setText(f)
def Converteither(self):
if self.lineEdit.text() == "":
f = str(float(self.lineEdit_2.text()) * (9/5) - 32)
self.lineEdit.setText(f)
else:
c = str(float(self.lineEdit.text()) * (9/5) + 32 )
self.lineEdit_2.setText(c)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
删除 def Celsius_Line(self) 和 Fahrenheit_Line(self) 并将按钮与控件连接
控制;
def control(self):
#I did not look at the formula may be wrong.
try:
if self.lineEdit.text() == "" and self.lineEdit_2.text() != "":
c = str(float(self.lineEdit.text())*(9/5) + 32)
self.lineEdit_2.setText(c)
elif self.lineEdit_2.text() == "" and self.lineEdit.text() != "":
f = str(float(self.lineEdit.text())*(9/5) - 32)
self.lineEdit_2.setText(f)
else:
print("please fill only one box.") # you can change that
except:
print('please enter a number') # you can change that
根据 Furkan Bilgin 的建议,我输入了正确的公式并更正了摄氏部分的 lineEdit.serText:
def control(self):
# With correct formulas
try:
if self.lineEdit.text() == "" and self.lineEdit_2.text() != "":
c = str((float(self.lineEdit_2.text())-32)*(5/9))
self.lineEdit.setText(c)
elif self.lineEdit_2.text() == "" and self.lineEdit.text() != "":
f = str(float(self.lineEdit.text())*(9/5) + 32)
self.lineEdit_2.setText(f)
else:
print("please fill only one box.") # you can change that
except:
print('please enter a number') # you can change that
我刚开始使用 PyQt5 和 so far I'm able to convert celsius to Fahrenheit but I'm a little confused on how to convert to celsius when I type a value in the Fahrenheit lineEdit box, example。如何检查任一框中是否有值并使用单个按钮将其转换?
temperature converter
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(299, 140)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 40, 121, 21))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(160, 40, 121, 21))
self.lineEdit_2.setObjectName("lineEdit_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 70, 75, 23))
self.pushButton.setObjectName("pushButton")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(160, 20, 61, 16))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(10, 20, 61, 16))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(140, 40, 21, 20))
self.label_4.setObjectName("label_4")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 299, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.Celsius_Line)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Convert"))
self.label_2.setText(_translate("MainWindow", "Fahrenheit"))
self.label_3.setText(_translate("MainWindow", "Celsius"))
self.label_4.setText(_translate("MainWindow", "="))
def Celsius_Line(self):
c = str(float(self.lineEdit.text()) * (9/5) + 32 )
self.lineEdit_2.setText(c)
def Fahrenheit_Line(self):
f = str(float(self.lineEdit_2.text()) * (9/5) - 32)
self.lineEdit.setText(f)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我不太明白问题出在哪里。
但据我了解,您想检查 "lineEdit" 部分是否为空。
if lineEdit.text() != '':
如果这样做,如果 "lineEdit" 为空,将返回 "True",您可以相应地更正代码。
修改代码,如果摄氏度为空,则从华氏度转换。否则 cel 到华氏度
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(299, 140)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 40, 121, 21))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(160, 40, 121, 21))
self.lineEdit_2.setObjectName("lineEdit_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 70, 75, 23))
self.pushButton.setObjectName("pushButton")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(160, 20, 61, 16))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(10, 20, 61, 16))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(140, 40, 21, 20))
self.label_4.setObjectName("label_4")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 299, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.Converteither) #Mod here
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Convert"))
self.label_2.setText(_translate("MainWindow", "Fahrenheit"))
self.label_3.setText(_translate("MainWindow", "Celsius"))
self.label_4.setText(_translate("MainWindow", "="))
def Celsius_Line(self):
c = str(float(self.lineEdit.text()) * (9/5) + 32 )
self.lineEdit_2.setText(c)
def Fahrenheit_Line(self):
f = str(float(self.lineEdit_2.text()) * (9/5) - 32)
self.lineEdit.setText(f)
def Converteither(self):
if self.lineEdit.text() == "":
f = str(float(self.lineEdit_2.text()) * (9/5) - 32)
self.lineEdit.setText(f)
else:
c = str(float(self.lineEdit.text()) * (9/5) + 32 )
self.lineEdit_2.setText(c)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
删除 def Celsius_Line(self) 和 Fahrenheit_Line(self) 并将按钮与控件连接 控制;
def control(self):
#I did not look at the formula may be wrong.
try:
if self.lineEdit.text() == "" and self.lineEdit_2.text() != "":
c = str(float(self.lineEdit.text())*(9/5) + 32)
self.lineEdit_2.setText(c)
elif self.lineEdit_2.text() == "" and self.lineEdit.text() != "":
f = str(float(self.lineEdit.text())*(9/5) - 32)
self.lineEdit_2.setText(f)
else:
print("please fill only one box.") # you can change that
except:
print('please enter a number') # you can change that
根据 Furkan Bilgin 的建议,我输入了正确的公式并更正了摄氏部分的 lineEdit.serText:
def control(self):
# With correct formulas
try:
if self.lineEdit.text() == "" and self.lineEdit_2.text() != "":
c = str((float(self.lineEdit_2.text())-32)*(5/9))
self.lineEdit.setText(c)
elif self.lineEdit_2.text() == "" and self.lineEdit.text() != "":
f = str(float(self.lineEdit.text())*(9/5) + 32)
self.lineEdit_2.setText(f)
else:
print("please fill only one box.") # you can change that
except:
print('please enter a number') # you can change that