def update_dropdowns(self, df): # 使用来自 DataFrame 的唯一区域更新下拉菜单 self.areas = [\'none\'] self.areas.extend( df[df.columns[1]].unique()) ...
def update_dropdowns(self, df):
# Update dropdown menus with unique areas from the DataFrame
self.areas = ["none"]
self.areas.extend( df[df.columns[1]].unique())
self.gui_components['area_dropdown_1'].clear()
self.gui_components['area_dropdown_2'].clear()
self.gui_components['area_dropdown_3'].clear()
print("mitte update dropdown")
for area in self.areas:
print("start for loop")
self.gui_components['area_dropdown_1'].addItem(area)
print("after_1")
self.gui_components['area_dropdown_2'].addItem(area)
print("after_2")
self.gui_components['area_dropdown_3'].addItem(area)
print("after_3")
所有区域下拉菜单的创建方式完全相同。调用此函数时,将打印以下内容:
如您所见,它被多次调用,但从不打印最后一条语句,并抛出最大递归深度错误。因此,我假设错误发生在最后一次 self.gui_components 调用中。
因此我将其注释掉,它完全正常工作,只是 GUI 中缺少一个组合框。但没有错误。奇怪的是,以下所有代码都有效:
def update_dropdowns(self, df):
# Update dropdown menus with unique areas from the DataFrame
self.areas = ["none"]
self.areas.extend( df[df.columns[1]].unique())
self.gui_components['area_dropdown_1'].clear()
self.gui_components['area_dropdown_2'].clear()
self.gui_components['area_dropdown_3'].clear()
print("mitte update dropdown")
for area in self.areas:
print("start for loop")
self.gui_components['area_dropdown_1'].addItem(area)
print("after_1")
self.gui_components['area_dropdown_2'].addItem(area)
print("after_2")
#self.gui_components['area_dropdown_3'].addItem(area)
print("after_3")
def update_dropdowns(self, df):
# Update dropdown menus with unique areas from the DataFrame
self.areas = ["none"]
self.areas.extend( df[df.columns[1]].unique())
self.gui_components['area_dropdown_1'].clear()
self.gui_components['area_dropdown_2'].clear()
self.gui_components['area_dropdown_3'].clear()
print("mitte update dropdown")
for area in self.areas:
print("start for loop")
self.gui_components['area_dropdown_1'].addItem(area)
print("after_1")
#self.gui_components['area_dropdown_2'].addItem(area)
print("after_2")
self.gui_components['area_dropdown_3'].addItem(area)
print("after_3")
def update_dropdowns(self, df):
# Update dropdown menus with unique areas from the DataFrame
self.areas = ["none"]
self.areas.extend( df[df.columns[1]].unique())
self.gui_components['area_dropdown_1'].clear()
self.gui_components['area_dropdown_2'].clear()
self.gui_components['area_dropdown_3'].clear()
print("mitte update dropdown")
for area in self.areas:
print("start for loop")
#self.gui_components['area_dropdown_1'].addItem(area)
print("after_1")
self.gui_components['area_dropdown_2'].addItem(area)
print("after_2")
self.gui_components['area_dropdown_3'].addItem(area)
print("after_3")
输出如下:
但最终它不起作用。我也尝试过这个:
# Initialization of the controller with the view, file dialog, and optional CSV file path and displayed table name
self.table_names = [] # List to store table names
super().__init__()
self.view = view # Reference to the view component
self.file_dialog = file_dialog # Reference to the file dialog component
self.displayed_table_name = displayed_table_name # Name of the table currently displayed
self.displayed_df = pd.DataFrame() # DataFrame for the displayed table
self.indicator_flag = True
self.updated_df = pd.DataFrame() # DataFrame for the updated table
self.source_df = pd.DataFrame() # Dataframe for the source table
self.indicator_df = pd.DataFrame() #Dataframe for the indicatior table
self.clicked_cells = [] # Set to keep track of clicked cells
self.indicator_path = None
self.source_path = None
self.indicator_name = None
self.source_name = None
self.areas = ["none"]
# GUI components and their corresponding view elements
self.gui_components = {
"main_table": self.view.main_table,
"updated_table": self.view.updated_table,
"substring_input":self.view.substring_input,
"area_dropdown_1": self.view.area_dropdown_1,
"area_dropdown_2": self.view.area_dropdown_2,
"area_dropdown_3": self.view.area_dropdown_3,
}
# Event handler for cell editing
def cell_edited(item):
if (item.row(),item.column()) in self.clicked_cells:
print("wuhu")
self.displayed_df.iloc[item.row(), item.column()] = item.text()
print("hello")
# Update the dropdown menus with the updated DataFrame
self.update_dropdowns(self.displayed_df)
print("qewrqewrqwerqrew")
#TODO: indicator source check
# Event handler for cell activation
def cell_activated(row,column):
self.clicked_cells.append((row,column))
# Event handler for table button clicks
def table_button_handler(item,name,bool):
if item.column() == 2 and self.indicator_flag:
self.open_information(str(name))
elif item.column() == 3 and self.indicator_flag:
self.open_source(str(name))
elif bool:
self.add_to_calc_tool(name)
# Connect buttons to their respective handlers
self.gui_components['delete_row_button'].clicked.connect(lambda: self.delete_row())
self.gui_components['export_button'].clicked.connect(lambda: self.export_filtered_table())
self.gui_components['import_button'].clicked.connect(lambda: self.import_data())
self.gui_components['main_table'].itemChanged.connect(cell_edited) ##
self.gui_components['main_table'].cellClicked.connect(cell_activated) ##
self.gui_components['main_table'].itemDoubleClicked.connect(lambda item:table_button_handler(item,self.gui_components['main_table'].item(item.row(), 0).text(),False))
self.gui_components['area_dropdown_1'].currentTextChanged.connect(lambda: self.apply_filters())
self.gui_components['area_dropdown_2'].currentTextChanged.connect(lambda: self.apply_filters())
self.gui_components['area_dropdown_3'].currentTextChanged.connect(lambda: self.apply_filters())
self.gui_components['substring_input'].textChanged.connect(lambda: self.apply_filters())
def update_dropdowns(self, df):
# Update dropdown menus with unique areas from the DataFrame
self.areas = ["none"]
self.areas.extend( df[df.columns[1]].unique())
self.gui_components['area_dropdown_1'].clear()
self.gui_components['area_dropdown_2'].clear()
self.gui_components['area_dropdown_3'].clear()
print("mitte update dropdown")
for area in self.areas:
print("start for loop")
self.gui_components['area_dropdown_1'].addItem(area)
print("after_1")
self.gui_components['area_dropdown_2'].addItem(area)
print("after_2")
self.gui_components['area_dropdown_3'].addItem(area)
print("after_3")
#self.update_last_dropdown(self.areas)
#def update_last_dropdown(self,areas):
# for area in areas:
# self.gui_components['area_dropdown_3'].addItem(area)
def apply_filters(self):
# Apply filters to the displayed DataFrame based on user input
# Get the user input from the GUI componentsa
substring = self.gui_components['substring_input'].text()
area_1 = self.gui_components['area_dropdown_1'].currentText()
area_2 = self.gui_components['area_dropdown_2'].currentText()
area_3 = self.gui_components['area_dropdown_3'].currentText()
# Filter by substring if provided
if substring != "":
updated_df = self.displayed_df[self.displayed_df['name'].str.contains(substring, case=False, na=False)]
# Filter by areas if selected
selected_areas = [area for area in [area_1, area_2, area_3] if area != "none"]
if selected_areas: #["economic","ecological"]
updated_df = self.displayed_df[self.displayed_df['area'].isin(selected_areas)]
else:
updated_df = self.displayed_df
# Update the controller's updated DataFrame
self.updated_df = updated_df.copy().drop_duplicates(inplace=True)
# Display the filtered DataFrame in the updated table
self.display_table(table_to_display_to = "updated_table", dataframe = updated_df)
这将会引发与开始时相同的错误。