Listing Members and deleting on selection

Member Login System

Display all the members from the MySQL database table. Any member can be selected and deleted by using a button. Tkinter Treeview is used to display the list and delete the members
Members listing in Login system
This is Part 3 of three part script.
The variable my_conn is declared before the function and it holds the database connection string.
We used messagebox to display confirmation message and showing the result of delete operation.
After getting confirmation of deletion from backend MySQL database, the row from the Treeview is removed.
trv.delete(selected_item)
##### Listing users with select and delete ###
def user_list():
    my_w_child3=Toplevel(my_w) # Child window 
    my_w_child3.geometry("300x300")  # Size of the window 
    my_w_child3.title("www.plus2net.com")
    from tkinter import messagebox as msg
    # display the list
    
    def my_show():
        r_set=my_conn.execute('''SELECT * from mem LIMIT 0,10''');
        trv=ttk.Treeview(my_w_child3,selectmode='browse')
        trv.grid(row=1,column=1,padx=20,pady=20)
        trv["columns"]=("1","2","3","4")
        trv['show']='headings'

        trv.column("1",width=30,anchor='c')
        trv.column("2",width=80,anchor='c')
        trv.column("3",width=80,anchor='c')
        trv.column("4",width=80,anchor='c')
    
        trv.heading("1",text="id")
        trv.heading("2",text="Name")
        trv.heading("3",text="Userid")
        trv.heading("4",text="Password")

        for dt in r_set:
            trv.insert("",'end',iid=dt[0],
                       values=(dt[0],dt[1],dt[2],dt[3]))
        b1 = tk.Button(my_w_child3, text='delete row', width=20,bg='yellow',command=lambda: delete())
        b1.grid(row=2,column=1)
    
        def delete():
            selected_item = trv.selection()[0]
            my_var=msg.askyesnocancel("Delete ?","Delete id:"+str(selected_item),icon='warning',default='no',parent=my_w_child3)
            if my_var: # True if yes button is clicked
                query="DELETE FROM  mem WHERE id=%s"
                rs=my_conn.execute(query,selected_item)
                if(rs.rowcount==1):
                    trv.delete(selected_item)
                    msg.showerror("Deleted ","No of records deleted = " + str(rs.rowcount),parent=my_w_child3)
            #my_conn.commit()
    
    my_show()
There are other two parts, one for listing and other for registration.
Main Login script Member Registration Member Login
Download login-system.ipynb file ( zip )


SQL DUMP
CREATE TABLE IF NOT EXISTS `mem` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `userid` varchar(15) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `password` varchar(20) NOT NULL DEFAULT '',
  UNIQUE KEY `id` (`id`),
  UNIQUE KEY `usrid` (`userid`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `mem`
--

INSERT INTO `mem` (`id`, `name`, `userid`, `password`) VALUES
(1, 'smo1', 'smo123', 'test123'),
(2, 'smo2', 'smo213', 'test213'),
(3, 'smo3', 'smo312', 'test312');


plus2net.com



Post your comments , suggestion , error , requirements etc here





Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer