Both buttons opens same table in csv problem? Both buttons opens same table in csv problem? tkinter tkinter

Both buttons opens same table in csv problem?


Below is a simple example based on your posted code:

import tkinter as tkfrom tkinter import ttkfrom csv import DictReaderclass App(tk.Tk):  def __init__(self):    super().__init__()    mainframe = tk.Frame(self)    mainframe.pack(fill=tk.BOTH, expand=1)    mainframe.rowconfigure(0, weight=1)    mainframe.columnconfigure(0, weight=1)    self.frames = {}    for f in (main_page, second_year):      frame = f(mainframe, self)      frame.grid(row=0, column=0, sticky='nsew')      self.frames[f] = frame    self.show_frame(main_page)  def show_frame(self, fclass):      frame = self.frames[fclass]      frame.tkraise()      return frameclass main_page(tk.Frame):  def __init__(self, parent, controller):    super().__init__(parent)    self.controller = controller    label_frame = tk.Frame(self)    label_frame.pack(fill=tk.BOTH, expand=1)    first_year_button = tk.Button(label_frame, text='First Year',                                  width=20, command=lambda: self.select_year(1))    first_year_button.grid(row=0, column=0, padx=10, pady=(10,0))    second_year_button = tk.Button(label_frame, text='Second Year',                                   width=20, command=lambda: self.select_year(2))    second_year_button.grid(row=0, column=1, padx=10, pady=(10,0))  def select_year(self, year):    frame = self.controller.show_frame(second_year)    frame.set_year(year)class second_year(tk.Frame):  def __init__(self, parent, controller):    super().__init__(parent)    columns = ('Reg #', 'Student Name', 'Father Name', 'Gender',               'Class', 'Session', 'Admin. Fee', 'Monthly Fee', 'Dues')    self.tree = ttk.Treeview(self, show='headings', columns=columns)    self.tree.pack(fill=tk.BOTH, expand=1)    for col in columns:      self.tree.heading(col, text=col)      self.tree.column(col, width=100, anchor='c')  def set_year(self, year):    if year == 1:      filter = '09th'    elif year == 2:      filter = 'Second Year'    else:      filter = None    if filter:      with open('registration.csv', newline='') as f:        reader = DictReader(f, delimiter=',')        for row in reader:          if row['Class Name'] == filter:            registration_no= row['Registration No']            student_name = row['Student Name']            f_name = row['Father Name']            gender = row['Gender']            class_name = row['Class Name']            class_session = row['Class Session']            admission_date = row['Admission Date']            monthly_fee = row['Monthly Fee']            dues_payment = row['Dues']            self.tree.insert('', 0, values=(registration_no, student_name, f_name, gender,                                            class_name, class_session, admission_date,                                            monthly_fee, dues_payment))app = App()app.mainloop()


Follow this kind of Structure, MainFrameClass is your old class that is coming from Library or creating an old frame for you. it has some variables that are under use inside that frame. Your task is to create new class, inherit old one into new. create new method through which you can only change those variables that you want to change inside frame.

class MainFrameClass:    def __init__(self, name):        self.name = name    def say_hi(self):        print("Hi, I am " + self.name)class UpdatedClass(MainFrameClass):    def show_name(self):        print("Hi, I am " + self.name)    def update_name(self,new_name):        self.name = new_namex = MainFrameClass("Old Variable Data Coming from Old Class")y = UpdatedClass("New Variable Data Given to New Class")y.show_name()y.update_name('New Variable Data That was Updated Again')y.show_name()