美好的一天@大家。我希望你今天过得愉快?我也非常想感谢所有为帮助我解决我上次发帖时遇到的问题做出贡献的人。 今天,我遇到了一个问题,它与我用全能的 for 循环为我生成的一堆按钮有关。按钮将沿着侧面标签生成,如下所示:对于学生 A => nameof_studentA_label、Scoreof_studentA 以及行尾按钮 => nameof_studentB_label、Scoreof_studentB 和行尾按钮。 ………… 问题是我无法让他们的活动正常进行。具体来说,每当我单击除第一个按钮之外的任何按钮时,我都不会执行任何操作。
我想要做的是从 sqlite 数据库中获取记录行并显示字段,带有标签“显示记录”事件的按钮将调用我名为“view_student_exam_record”的方法,并以某种方式获取与来自与按钮匹配的数据库。当我点击任何一个 for-loop- generated-loop 后,该事件将会触发。
下面是使用 for 循环获取记录并生成标签和按钮的代码。
def search_exam_record(self, event):
count=0
if self.search_exam_class_name_text.GetValue()!="" and self.search_exam_term_name_text.GetValue()!="" and self.search_exam_session_name_text.GetValue()!="":
#search_exam_record = SchoolManagementSystem.retrieve_data(SchoolManagementSystem, "exam", "*", "where class=? and term =? and session=?", (self.search_exam_class_name_text.GetValue().lower(), self.search_exam_term_name_text.GetValue(), self.search_exam_session_name_text.GetValue()))
search_exam_record=cursor.execute("select *, rank() over(order by total_scores desc) as rank from exam where class=? and term=? and session=? ", (self.search_exam_class_name_text.GetValue().lower(), self.search_exam_term_name_text.GetValue(), self.search_exam_session_name_text.GetValue()))
self.sub_dict = {}
count_list = []
self.student_name_list = []
self.student_id_list = []
self.total_score_list = []
self.average_list = []
self.rank_list = []
self.view_button_list = []
self.button_id = []
for exam_record in search_exam_record:
student_name = exam_record['studentname']
studentid = exam_record['student_id']
subject_dict_str = exam_record['scores']
subject_dict = eval(subject_dict_str)
self.subject_list = list(subject_dict.keys())
self.subject_score_list = list(subject_dict.values())
total_score= exam_record['total_scores']
rank = exam_record['rank']
rank = str(rank)
average_score = total_score/len(subject_dict)
average_score = round(average_score, 2)
if rank[-1]=='1':
rank = rank+'st'
elif rank[-1]=='2':
rank = rank+'nd'
elif rank[-1] =='3':
rank = rank+'rd'
else:
rank = rank+'th'
self.button_id.append(wx.NewId())
name_heading = wx.StaticText(self.panel2, label="Name")
id_heading = wx.StaticText(self.panel2, label="Student Id")
total_heading = wx.StaticText(self.panel2, label="Total Score")
average_heading = wx.StaticText(self.panel2, label="Average")
position_heading = wx.StaticText(self.panel2, label="Position")
empty_heading = wx.StaticText(self.panel2, label="")
studentname_label = wx.StaticText(self.panel2, label=student_name)
studentid_label = wx.StaticText(self.panel2, label=str(studentid))
student_total_scores = wx.StaticText(self.panel2, label=subject_dict_str)
totalscore_label = wx.StaticText(self.panel2, label=str(total_score))
average_label = wx.StaticText(self.panel2, label=str(average_score))
rank_label = wx.StaticText(self.panel2, label=rank)
self.view_button = wx.Button(self.panel2, self.button_id[count], label="Display Record")
self.student_name_list.append(studentname_label)
self.student_id_list.append(studentid_label)
self.total_score_list.append(totalscore_label)
self.average_list.append(average_label)
self.rank_list.append(rank_label)
count_list.append(count)
self.view_button_list.append(self.view_button)
self.button_id.append(self.view_button_list[count].GetId())
self.view_button_list[count].Bind(wx.EVT_BUTTON, lambda event, count=count: self.view_student_exam_record(event, count, self.sub_dict))
self.sub_dict[self.view_button_list[count].GetId()]:[count+1, studentid_label.GetLabel(), studentname_label.GetLabel(), totalscore_label.GetLabel(), average_label.GetLabel(), rank_label.GetLabel() ]
if count==0:
self.search_exam_result_grid_sizer.AddMany(
[
(name_heading, 0, wx.ALL, 5),
(id_heading, 0, wx.ALL, 5),
(total_heading, 0, wx.ALL, 5),
(average_heading, 0, wx.ALL, 5),
(position_heading, 0, wx.ALL, 5),
(empty_heading, 0, wx.ALL, 5),
(studentname_label, 0, wx.ALL, 5),
(studentid_label, 0, wx.ALL, 5),
(totalscore_label, 0, wx.ALL, 5),
(average_label, 0, wx.ALL, 5),
(rank_label, 0, wx.ALL, 5),
(self.view_button, 0, wx.ALL, 5),
]
)
else:
self.search_exam_result_grid_sizer.AddMany(
[
(studentname_label, 0, wx.ALL, 5),
(studentid_label, 0, wx.ALL, 5),
(totalscore_label, 0, wx.ALL, 5),
(average_label, 0, wx.ALL, 5),
(rank_label, 0, wx.ALL, 5),
(self.view_button, 0, wx.ALL, 5),
]
)
count+=1
else:
print("Please fill the required field")
self.panel2.SetSizer(self.search_exam_result_grid_sizer)
print(len(self.sub_dict))
下面是从 for 循环生成按钮触发事件的代码
def view_student_exam_record(self, event, index, sub_dict):
button_id = event.GetEventObject()
button_id = button_id.GetId()
print(button_id)
counter=0
search_student_record=cursor.execute("select student_id, dob, parentaddress, gender, place, lgaoforigin, stateoforigin, nationality, parent, passportdir from student where student_id=? ", (index,))
print(index)
search_student_exam_record=cursor.execute("select student_id, scores, total_scores, class, term, session from exam where student_id=? ", (index,))
for student_record in search_student_record:
counter+1
student_id = student_record['student_id']
print(student_id)
student_dob = student_record['dob']
student_gender = student_record['gender']
student_town_village = student_record['place']
student_lga = student_record['lgaoforigin']
student_state = student_record['stateoforigin']
student_nationality = student_record['nationality']
parent = student_record['parent']
passportfolder = student_record['passportdir']
studentname_label = wx.StaticText(self, label=self.student_name_list[index].GetLabel())
studentid_label = wx.StaticText(self, label=str(self.student_id_list[index].GetLabel()))
studentdob_label = wx.StaticText(self, label=student_dob)
studentgender_label = wx.StaticText(self, label=student_gender)
studenttown_label = wx.StaticText(self, label=student_town_village)
studentlga_label = wx.StaticText(self, label=student_lga)
studentstate_label = wx.StaticText(self, label=student_state)
studentnationality_label = wx.StaticText(self, label=student_nationality)
parent_label = wx.StaticText(self, label=parent)
self.right_grid_box.AddMany(
[
(studentname_label, 0, wx.ALL, 5),
(studentid_label, 0, wx.ALL, 5),
(studentdob_label, 0, wx.ALL, 5),
(studentgender_label, 0, wx.ALL, 5),
(studenttown_label, 0, wx.ALL, 5),
#(studenttown_label, 0, wx.ALL, 5),
(studentlga_label, 0, wx.ALL, 5),
(studentstate_label, 0, wx.ALL, 5),
(studentnationality_label, 0, wx.ALL, 5),
(parent_label, 0, wx.ALL, 5),
]
)
print(index)
self.sub_lis = []
if len(search_student_exam_record.fetchall())>0:
pass
else:
pass
print(search_student_exam_record.fetchall())
sub_index=0
for subject in self.subject_list:
subject_label = wx.StaticText(self, label=self.subject_list[sub_index])
test1_label = wx.StaticText(self, label=str(self.subject_score_list[sub_index][0]))
test2_label = wx.StaticText(self, label=str(self.subject_score_list[sub_index][1]))
test3_label = wx.StaticText(self, label=str(self.subject_score_list[sub_index][2]))
exam_label = wx.StaticText(self, label=str(self.subject_score_list[sub_index][3]))
total_exams_score_label = wx.StaticText(self, label=str(self.subject_score_list[sub_index][4]))
total_score_label = wx.StaticText(self, label=str(self.total_score_list[sub_index].GetLabel()))
average_score_label = wx.StaticText(self, label=str(self.average_list[sub_index].GetLabel()))
position_label = wx.StaticText(self, label=self.rank_list[sub_index].GetLabel())
self.right_grid_box2.AddMany(
[
(subject_label, 0, wx.ALL, 5),
(test1_label, 0, wx.ALL, 5),
(test2_label, 0, wx.ALL, 5),
(test3_label, 0, wx.ALL, 5),
(exam_label, 0, wx.ALL, 5),
(total_exams_score_label, 0, wx.ALL, 5),
(total_score_label, 0, wx.ALL, 5),
(average_score_label, 0, wx.ALL, 5),
(position_label, 0, wx.ALL, 5),
]
)
sub_index+=1
self.SetSizer(self.right_box_sizer)
我希望我足够清楚。
这是一个简单的应用程序来展示其原理。您需要为每个按钮分配一个 id,并且可以使用 event.GetEventObject().GetId()
在绑定事件中检索它import wx
class MainFrame(wx.Frame):
def __init__(self, *args, **kwargs):
super().__init__(None, *args, **kwargs)
self.Title = 'Wx App'
self.panel = MainPanel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.panel)
self.SetSizer(sizer)
self.Center()
self.Show()
class MainPanel(wx.Panel):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
sizer = wx.BoxSizer(wx.VERTICAL)
for index in range(5):
button = wx.Button(self, label=f'Button {index}', id=index)
button.Bind(wx.EVT_BUTTON, self.get_record)
sizer.Add(button)
self.SetSizer(sizer)
def get_record(self, event):
print(event.GetEventObject().GetId())
if __name__ == '__main__':
wx_app = wx.App()
MainFrame()
wx_app.MainLoop()