【发布时间】:2019-06-21 08:24:24
【问题描述】:
已编辑
我的 QComboBox 中有两个选项,分别是“英尺”和“米”。 每次我在 GUI 上选择第一个选项时,或者如果我没有选择这两个选项中的任何一个,应用程序就会崩溃(python 停止工作),因为该值已经在“英尺”中。
我有以下经过编辑的代码行;
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.value = QtWidgets.QLineEdit()
self.unit = QtWidgets.QComboBox()
self.convert = QtWidgets.QPushButton()
self.setLayout(QtWidgets.QVBoxLayout())
self.value.setObjectName("value")
self.layout().addWidget(self.value)
self.layout().addWidget(self.unit)
self.unit.addItems(["feet", "meters"])
self.layout().addWidget(self.convert)
self.convert.setText("Convert")
self.unit.currentIndexChanged.connect(self.unit_select)
self.convert.clicked.connect(self.convert_click)
# UNIT SELECT
def unit_select(self):
current_index = self.unit.currentIndex()
if current_index == 0:
num = float(self.value.text()) * 0.3047972654
elif current_index == 1:
num = float(self.value.text()) * 1
self.final = num
# CONVERT VALUE
def convert_click(self):
print("Converted value =", self.final)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
我需要将“脚”设置为 QComboBox 的默认值。
【问题讨论】:
-
请说明您在哪里遇到崩溃以及如何初始化/连接您的组合框,这里的代码不足以帮助您。
-
我已经编辑了问题