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()