简体   繁体   中英

Problem with refreshing the content with python Tkinter , sqlite3

So im building a note app which contains 5 pages (login,register,welcoming,text_writting,archived_text) the app can take multiple users and each user have his own notes stored in the database the problem when i disconnect from a user and i want to switch to the other user it keeps the old user infos without refreshing and showing the new one,it shows only when i close the app and reopen it Plus -> when i check the database everything is going well the date do update the problem is just in the GUI

class main_app(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        data_baseconnect=sqlite3.connect('NOTES_DB.db')
        data_cursor=data_baseconnect.cursor()
        data_cursor.execute('SELECT current_state from LOGINS_STATE')
        data_baseconnect.commit()
        Current_person_state = data_cursor.fetchall()
        for frame_class in (Login_page,Register_page, Welcoming_page,notes_page1,notes_page2):
            frame = frame_class(container, self)
            self.frames[frame_class] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        if Current_person_state[0][0]==0: 
         self.show_frame(Login_page)
        else :
          self.update()
          self.show_frame(Welcoming_page)
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
#Login page
class Login_page(tk.Frame):
    def __init__(self, parent, controller):
        def user_connect() :
#Login_page_User_name_Entry is an entry when the user type the new person he want to connect
#through // this user is already in the data base 
                    global Current_userV2
                    database_connection=sqlite3.connect('NOTES_DB.db')
                    data_currsor=database_connection.cursor()
                    Login_page_Error_connect.config(text='')
                    Current_userV2=str(Login_page_User_name_Entry.get())
                    data_currsor.execute("UPDATE LOGINS_STATE SET current_user=(?),current_state=(?)",((str(Login_page_User_name_Entry.get())),('1')))
                    database_connection.commit()
                    controller.show_frame(Welcoming_page)            
        tk.Frame.__init__(self,parent,bg='#EDD01C')
        Login_page_connect_button=Button(Login_page_frame,text="Connect",width=20,height=2,bg='green',command=user_connect)
        Login_page_connect_button.place(x=80,y=300)
      
.....
class Welcoming_page(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent,bg='#EDD01C')
        def Leave_button() : 
            global Current_userV2
            database_connect=sqlite3.connect('NOTES_DB.db')
            data_cursor=database_connect.cursor()
            data_cursor.execute('UPDATE LOGINS_STATE set current_user=NULL,current_state=0')
            database_connect.commit()
            Current_userV2=''
            controller.show_frame(Login_page)
  disconnect_button=Button(self,text="Disconnect",bg='red',command=Leave_button,font=('arial',15))
        disconnect_button.place(x=350,y=500)
 ....

i figured out here s how: just changing in the main_app class:

class main_app(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.container = tk.Frame(self)
        self.container.pack(fill="both", expand=True)
        self.current_frame = None
        data_baseconnect=sqlite3.connect('NOTES_DB.db')
        data_cursor=data_baseconnect.cursor()
        data_cursor.execute('SELECT current_state from LOGINS_STATE')
        data_baseconnect.commit()
        Current_person_state = data_cursor.fetchall()
        if Current_person_state[0][0]==0: 
         self.show_frame(Login_page)
        else :
          self.show_frame(Welcoming_page)
    def show_frame(self, new_frame_class):
        if self.current_frame:
            self.current_frame.destroy()
        self.current_frame = new_frame_class(self.container, controller=self)
        self.current_frame.pack(fill="both", expand=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM