非常抱歉卡住了,以下是修改后的完整代码:
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QComboBox
import pandas as pd
class AssetManagementSystem(QMainWindow):
def __init__(self):
super().__init__()
self.comboboxes = [
self.comboBox,
self.comboBox_2,
self.comboBox_3,
self.comboBox_4,
self.comboBox_5
]
self.init_ui()
def init_ui(self):
# 初始化界面布局等
self.centralWidget = QWidget(self)
self.layout = QVBoxLayout(self.centralWidget)
for combobox in self.comboboxes:
combobox.currentIndexChanged.connect(self.handle_combobox_change)
self.setCentralWidget(self.centralWidget)
self.setGeometry(300, 300, 400, 300)
self.setWindowTitle('Asset Management System')
self.show()
def setup_area_selection(self, comboboxes, data_model):
levels = len(comboboxes)
df = pd.read_excel(data_model)
for i, combobox in enumerate(comboboxes):
current_level = i + 1
selected_code = ''
previous_combobox = comboboxes[i - 1] if i > 0 else None
if previous_combobox:
selected_code = str(previous_combobox.currentData())
filtered_data = df[df['区划代码'].str.startswith(selected_code) & (df['级别'] == current_level)]
combobox.clear()
combobox.addItem("请选择")
for index, row in filtered_data.iterrows():
name = row['名称']
code = row['区划代码']
combobox.addItem(name, code)
def handle_combobox_change(self, index):
for i, combobox in enumerate(self.comboboxes):
if i > index:
combobox.setCurrentIndex(0)
if index < len(self.comboboxes) - 1:
self.setup_area_selection(self.comboboxes[index + 1:], self.Region_file)
if __name__ == '__main__':
app = QApplication([])
system = AssetManagementSystem()
system.setup_area_selection(system.comboboxes[:1], "行政区划代码库.xlsx")
app.exec()
您可以尝试在您的项目中运行并查看是否能够正常工作。请确保将Excel文件路径传递给`setup_area_selectio
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |