简体   繁体   English

使用 python Tkinter,sqlite3 刷新内容时出现问题

[英]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因此,我正在构建一个包含 5 个页面(登录、注册、欢迎、text_writting、archived_text)的便笺应用程序,该应用程序可以容纳多个用户,并且每个用户都有自己的便笺存储在数据库中,当我与用户断开连接并且我想切换到另一个用户它保留旧用户信息而不刷新并显示新用户信息,它仅在我关闭应用程序并重新打开它时显示 Plus -> 当我检查数据库时一切顺利,更新日期问题只是在图形用户界面中

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:我想出了这里的方法:只需在 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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