from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QApplication,QWidgetclass MyWindow(QWidget):def __init__(self):super().__init__()self.ui = QUiLoader().load('trans.ui')self.ui.show()#stor data type dictionaryself.lengthVar = {'米':100, '千米':1000, '厘米':1, '分米':10}self.weightVar = {'克':1, '千克':1000}self.TypeDict = {'length':self.lengthVar,'weight':self.weightVar}self.ui.dataTypeComboBox.addItems(self.TypeDict.keys())self.ui.oneInputComboBox.addItems(self.lengthVar.keys())self.ui.twoInputComboBox.addItems(self.lengthVar.keys())def bind(self):self.dataTypeComboBox.currentTextChanged.connect(self.typeChanged)self.calcBtn.clicked.connect(self.calc)def calc(self):bigType = self.dataTypComboBox.currentText()# 获取第一个输入框的值value = self.oneInputEditLine.text()if value == '':returncurrentType = self.oneInputComboBox.currentText()transType = self.twoInputComboBox.currentText()standardization = float(value) * self.TypeDict[bigType][currentType]result = standardization / self.TypeDict[bigType][transType]self.originDataLabel.setText(f'{value} {currentType}')self.transDataLabel.setText(f'{result} {transType}')self.twoInputEditLine.setText(str(result))def typeChanged(self, text):self.oneInputComboBox.clear()self.twoInputComboBox.clear()self.oneInputComboBox.addItems(self.TypeDict[text].keys())self.twoInputComboBox.addItems(self.TypeDict[text].keys())# if __name__ =='name':
app = QApplication([])
window = MyWindow()
app.exec()
你现在的代码大致思路对,但有几个关键问题,导致计算部分跑不起来:
❌ 1. 控件访问方式不一致
在 __init__
里,你已经把 UI 加载到 self.ui
里了:
self.ui = QUiLoader().load('trans.ui')
但是在后面的代码里,你有些地方写的是 self.dataTypeComboBox
、self.calcBtn
,而不是 self.ui.dataTypeComboBox
。
👉 因为 所有 Designer 里的控件都属于 self.ui
,所以必须通过 self.ui.xxx
来访问。
比如:
self.dataTypeComboBox.currentTextChanged.connect(self.typeChanged)
要改成:
self.ui.dataTypeComboBox.currentTextChanged.connect(self.typeChanged)
同样地,在 calc
里:
bigType = self.dataTypComboBox.currentText() # ❌ 少了 e,还没加 self.ui
要改成:
bigType = self.ui.dataTypeComboBox.currentText()
❌ 2. bind()
没有被调用
你定义了 bind()
方法来连接信号槽,但是在 __init__
里没有调用。
所以按钮点了根本不会触发 calc()
。
要加一句:
self.bind()
❌ 3. 控件名字可能拼错
在 calc()
里:
bigType = self.dataTypComboBox.currentText()
这里应该是 dataTypeComboBox
,而不是 dataTypComboBox
(少了一个 e
)。
✅ 修改后的代码
我帮你整理一份能运行的版本:
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QApplication, QWidgetclass MyWindow(QWidget):def __init__(self):super().__init__()self.ui = QUiLoader().load('trans.ui')self.ui.show()# 存放数据字典self.lengthVar = {'米':100, '千米':1000, '厘米':1, '分米':10}self.weightVar = {'克':1, '千克':1000}self.TypeDict = {'length':self.lengthVar, 'weight':self.weightVar}# 初始化下拉框self.ui.dataTypeComboBox.addItems(self.TypeDict.keys())self.ui.oneInputComboBox.addItems(self.lengthVar.keys())self.ui.twoInputComboBox.addItems(self.lengthVar.keys())# ⭐ 别忘了绑定信号self.bind()def bind(self):self.ui.dataTypeComboBox.currentTextChanged.connect(self.typeChanged)self.ui.calcBtn.clicked.connect(self.calc)def calc(self):bigType = self.ui.dataTypeComboBox.currentText()# 获取第一个输入框的值value = self.ui.oneInputEditLine.text()if value == '':returncurrentType = self.ui.oneInputComboBox.currentText()transType = self.ui.twoInputComboBox.currentText()standardization = float(value) * self.TypeDict[bigType][currentType]result = standardization / self.TypeDict[bigType][transType]self.ui.originDataLabel.setText(f'{value} {currentType} = ')self.ui.transDataLabel.setText(f'{result} {transType}')self.ui.twoInputEditLine.setText(str(result))def typeChanged(self, text):self.ui.oneInputComboBox.clear()self.ui.twoInputComboBox.clear()self.ui.oneInputComboBox.addItems(self.TypeDict[text].keys())self.ui.twoInputComboBox.addItems(self.TypeDict[text].keys())app = QApplication([])
window = MyWindow()
app.exec()
。自己也,在git里做了一份代码点击查看
🔑 总结
-
用 QUiLoader 加载
.ui
时,所有控件都在self.ui
里。 -
信号绑定必须调用,比如
self.bind()
。 -
注意控件名拼写一致(不要漏字母)。