First of all, put the code here, and then have time to explain the problems in the program,
This small program knowledge to implement the function, and there is not much to the layout of the pursuit of
Some comments are not deleted, and there is no function to wrap up, looking at the whole
It should be a little messy.
The following is the complete code,
ImportTkinterImportPymysql fromTkinterImport* fromTkinterImportTtkwindow=tkinter. Tk ()#Create WindowWindow.title ("Phone book")#Set TitleWindow.geometry ('500x500+500+200')#Set the size offsetWindow.resizable (Width=false,height=false)#disable wide-heightDatabase = Pymysql.connect (host="localhost", user="Root", password="******", DB="Phonebook", port=3306, charset="UTF8") Listn=TTK. Treeview (window) listn=TTK. Treeview (window,show="headings", height=10,columns= ("a","b","C","D"))#listn["Columns"]= ("a", "B", "C", "D")Listn.column ("a", width=60) Listn.column ("b", width=100) Listn.column ("C", width=120) Listn.column ("D", width=200) listn.heading ("a", text="name") listn.heading ("b", text="Telephone") listn.heading ("C", text="Email") listn.heading ("D", text="Address") listn["SelectMode"]="Browse"defDeleteinfo (name,phone): Sql_delete=r"Delete from info where name= '%s '"R"and phone= '%s ';"%(name,phone) cur=database.cursor ()Try: Cur.execute (sql_delete) database.commit ()exceptException as E:database.rollback () cur.close ()defListall (): Sql_sel_all="SELECT * from info;"cur=database.cursor ()Try: Cur.execute (sql_sel_all) Results=Cur.fetchall ()#print ("name", "Phone", "Mailbox", "Address") #first delete the original node data in the table for_inchMap (Listn.delete,listn.get_children ("")): PassII=0 forRowinchResults:name=row[0] Phone=row[1] Mail=row[2] Address=row[3] #print (name,phone,mail,address)Listn.insert ("", ii,text=ii+1,values=(name,phone,mail,address)) II=ii+1exceptException as E:Raisee cur.close ()defInsertinfo (name,phone,mail=none,address=None): Sql_insert=r"INSERT INTO info (name,phone,"R"mail,address) VALUES ('%s ', '%s ', '%s ',"R"'%s ');"%(name,phone,mail,address) cur=database.cursor ()Try: Cur.execute (Sql_insert) database.commit ()exceptException as E:database.rollback () cur.close ()defNodes (): Notenote1="Description:"Notenote2="Enter your name and phone at least when you add a contact"Notenote3="mailbox and address can be omitted, you can add the same name"notenote4="but can not add the same name, the same telephone information"Notenote5="simply select the item you want to delete, then click"Notenote6="Delete contact button to delete"note1=label (window,text=notenote1) Note1.pack () Label (Window,text=notenote2). Pack () Label (Window,text=notenote3). Pack () Label (Window,text=notenote4). Pack () Label (Window,text=notenote5). Pack () Label (Window,text=notenote6). Pack ()Pass#Creating child windowsdefZwindow (): Zwin=Tkinter. Tk () Zwin.title ("Add") Zwin.geometry ('350x200+520+350') zwin.resizable (width=false,height=False) Ln=label (zwin,text="name") Lp=label (zwin,text="Telephone") Lm=label (zwin,text="Email") La=label (zwin,text="Address") Ln.grid (Row=0) Lp.grid (row=1) Lm.grid (Row=2) La.grid (Row=3) E1=Entry (Zwin) E2=Entry (Zwin) E3=Entry (Zwin) E4=Entry (Zwin) e1.grid (Row=0,column=1) E2.grid (Row=1,column=1) E3.grid (Row=2,column=1) E4.grid (Row=3,column=1) defInserti (): Insertinfo (E1.get (), E2.get (), E3.get (), E4.get ()) Zwin.destroy () Listall () Ybutto N=button (zwin,text='Determine', command=Inserti) Nbutton=button (zwin,text='Cancel', command=Zwin.destroy) Ybutton.grid (Row=4,column=1,padx=3) Nbutton.grid (Row=4,column=2)defDelete ():#listn.Items=listn.selection ()#returns the ID of the selected rowDelete_name=listn.item (items) ["Values"][0] Delete_phone=listn.item (items) ["Values"][1] #print (items) #Print (Listn.item (items)) #print (delete_name) #print (Delete_phone)deleteinfo (Delete_name,delete_phone) listall ()PassINSERTB=button (window,text='Add a contact person', command=Zwindow) Deleteb=button (window,text='Delete a contact', command=Delete) Listall () Listn.pack () Insertb.pack () Deleteb.pack () Nodes () Window.mainloop ()
Also attached is a code to practice connecting to the database:
#coding = Utf-8ImportPymysql#connecting to a databaseDatabase=pymysql.connect (host="localhost", user="Root", password="******", DB="Pydata", port=3306) cur=database.cursor ()Print("#1. Querying the database First") SQL="SELECT * FROM Info"Try: Cur.execute (SQL) Results=Cur.fetchall ()Print("name","ID") forRowinchResults:name=row[0] ID=row[1] Print(Name,id)exceptException as E:RaiseePrint("#2. Inserting Data") Sql_in_name=input ("Please enter the name you want to insert:") sql_in_id=input ("Please enter the ID you want to insert:") Sql_insert=r"INSERT into info (name,id) VALUES ('%s ', '%s ');"%(sql_in_name,sql_in_id)Try: Cur.execute (Sql_insert) database.commit ()exceptException as E:database.rollback ()Print("Verify Insert")Try: Cur.execute (SQL) Results=Cur.fetchall ()#print ("name", "ID") forRowinchResults:name=row[0] ID=row[1] Print(Name,id)exceptException as E:RaiseE
Python connection to MySQL database small phone book written