我正在使用此 Switch between two frames in tkinter 中的答案来创建我的 GUI。
对于第一帧 StartPage
,我尝试使用名为 DbConfig().fetch_data()
的函数显示数据库中的数据。
第二帧 NewStudentPage
用于插入新数据。
我试图弄清楚如何在 DbConfig().fetch_data()
中调用 StartPage
,如果该框架可见,或者在我单击 save (insert and redirects)
中的按钮 NewStudentPage
后,所以我可以与新插入的数据一起显示。
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()
答案 0 :(得分:1)
您在保存学生时尚未调用该函数。将以下代码添加到
save_data
类中的 NewStudentPage
函数。这将打印数据库表中的所有数据,即使是刚刚添加的数据。
data = DbConfig().fetch_data()
print(data)