1

I'm using the answer from this Switch between two frames in tkinter to create my GUI.

For the first frame StartPage I'm trying to display the data from database using a function called DbConfig().fetch_data().

The second frame NewStudentPage is used to insert new data.

I'm trying to figure out how to recall the DbConfig().fetch_data() in StartPage if that frame is visible or after I click the button save (insert and redirects) from the NewStudentPage so I could display the data along with the newly inserted.

class SampleApp(Tk):
  def __init__(self, *args, **kwargs):
    Tk.__init__(self, *args, **kwargs)

    container = Frame(self)
    container.pack(side="top", fill="both", expand=True)
    container.grid_rowconfigure(0, weight=1)
    container.grid_columnconfigure(0, weight=1)

    self.frames = {}

    for f in (StartPage, NewStudentPage):
      page_name = f.__name__
      frame = f(parent=container, controller=self)
      self.frames[page_name] = frame

      frame.grid(row=0, column=0, sticky="nsew")

    self.show_frames("StartPage")

  def show_frames(self, page_name):
    frame = self.frames[page_name]
    frame.tkraise()

class StartPage(Frame):
  def __init__(self, parent, controller):
    Frame.__init__(self, parent)

    self.controller = controller
    Label(self, text="StandBy").pack(side="top", fill="x", pady=10)
    
    data = DbConfig().fetch_data()  <<<<<<--------------------- THIS FUNCTION
    print(data)

    Button(self, text="add student", command=lambda: controller.show_frames("NewStudentPage")).pack()


class NewStudentPage(Frame):
  def __init__(self, parent, controller):
    Frame.__init__(self, parent)
    self.controller = controller

    Label(self, text="New Student", font=Font(size="12", weight="bold")).place(relx=0.5, rely=0.1, anchor=CENTER)

    self.course = StringVar()
    Label(self, text="course").place(x=40, y=40)
    Entry(self, textvariable=self.course, width=25).place(x=80, y=40)
    
    self.firstname = StringVar()
    Label(self, text="firstname").place(x=15, y=65)
    Entry(self, textvariable=self.firstname, width=15).place(x=50, y=65)

    self.lastname = StringVar()
    Label(self, text="lastname").place(x=160, y=65)
    Entry(self, textvariable=self.lastname, width=15).place(x=190, y=65)

    Button(self, text="back", command=lambda: controller.show_frames("StartPage")).place(x=110, y=120)
    Button(self, text="save", command=lambda: [self.save_data(), controller.show_frames("StartPage")]).place(x=150, y=120)
  
  def save_data(self):
    course = self.course.get()
    firstname = self.firstname.get()
    lastname = self.lastname.get()
    DbConfig().insert_student(course, firstname, lastname)

class DbConfig:
  def __init__(self):
    self.conn = sqlite3.connect("database/config.db")
    self.cursor = self.conn.cursor()
    self.create_table()

  def create_table(self):
    self.cursor.execute("""CREATE TABLE IF NOT EXISTS tbl_student (
          _id integer PRIMARY KEY,
          course text,
          firstname text,
          lastname text
        )""")

  def insert_student(self, course, firstname, lastname):
    self.cursor.execute("""INSERT INTO tbl_student
      (course, firstname, lastname)
      VALUES (?, ?, ?) """, (course, firstname, lastname))
    self.conn.commit()

  def fetch_data(self):
    data = self.cursor.execute("SELECT * FROM tbl_student").fetchall()
    return data


if __name__ == "__main__":
  app = SampleApp()
  app.mainloop()
Nellartsa
  • 29
  • 1
  • 7

1 Answers1

1

You have not called the function when you save the student. Add the following code to the save_data function in NewStudentPage Class. This will print all the data from the database table, even the once added right then.

data = DbConfig().fetch_data()
print(data)

  • I need to call it inside the `StartPage` cause the data should be displayed there. If I call it inside the `NewStudentPage` the updated data wouldn't be displayed in `StartPage`. I'm sorry if my question is a bit confusing I'll edit it. – Nellartsa May 16 '21 at 14:14
  • So you want to display the students on the main page? –  May 16 '21 at 16:05
  • I think you should use ttk.treeview –  May 16 '21 at 16:08
  • Yes but I can't recall the function to get the updated data. Ill try to read about treeview thank you – Nellartsa May 17 '21 at 04:37