SHARE
TWEET

Untitled

a guest Aug 19th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from tkinter import *
  2. import sqlite3
  3. import tkinter.ttk as ttk
  4. import tkinter.messagebox as tkMessageBox
  5.  
  6. root = Tk()
  7. root.title("Search")
  8. width = 500
  9. height = 400
  10. screen_width = root.winfo_screenwidth()
  11. screen_height = root.winfo_screenheight()
  12. x = (screen_width/2) - (width/2)
  13. y = (screen_height/2) - (height/2)
  14. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  15. root.resizable(0, 0)
  16.  
  17. #=====================================METHODS==============================================
  18. def Database(): # Database Connection/Creation
  19.     global conn, cursor
  20.     conn = sqlite3.connect("pythontut.db")
  21.     cursor = conn.cursor()
  22.     cursor.execute("CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT, firstname TEXT, lastname TEXT, address TEXT, age TEXT)")
  23.     cursor.execute("SELECT * FROM `member`")
  24.     if cursor.fetchone() is None:
  25.         cursor.execute("INSERT INTO `member` (firstname, lastname, address, age) VALUES('Clea', 'Hela', 'San Francisco', '20')")
  26.         cursor.execute("INSERT INTO `member` (firstname, lastname, address, age) VALUES('Thor', 'Jackson', 'San Aurora', '25')")
  27.         cursor.execute("INSERT INTO `member` (firstname, lastname, address, age) VALUES('Naruto', 'Uzumaki', 'Konoha', '18')")
  28.         cursor.execute("INSERT INTO `member` (firstname, lastname, address, age) VALUES('San', 'Juan', 'Peninsula', '55')")
  29.         cursor.execute("INSERT INTO `member` (firstname, lastname, address, age) VALUES('Juan', 'Dela Cruz', 'San Juan', '22')")
  30.         cursor.execute("INSERT INTO `member` (firstname, lastname, address, age) VALUES('Pedro', 'Bago', 'Bayan', '30')")
  31.         conn.commit()
  32.     cursor.execute("SELECT * FROM `member` ORDER BY `firstname` ASC")
  33.     fetch = cursor.fetchall()
  34.     for data in fetch:
  35.         tree.insert('', 'end', values=(data))
  36.  
  37.  
  38. def find(fstr):  # string to be searched
  39.     tree.delete(*tree.get_children())
  40.     if (fstr) != "":
  41.         cursor.execute("SELECT * FROM `member` WHERE `firstname`  LIKE ? ORDER BY `firstname`" , ('%'+fstr+'%',))
  42.         tree.selection_set(tree.tag_has(SEARCH.get()))
  43.     else:
  44.         cursor.execute("SELECT * FROM `member` ORDER BY `firstname` ASC")
  45.  
  46.     print(fstr)
  47.     fetch = cursor.fetchall()
  48.     for data in fetch:
  49.         tree.insert('', 'end', values=(data))
  50.  
  51. def onValidate(P):
  52.     print(P)
  53.     #f (P)!= "":
  54.     find(str(P))
  55.  
  56.     return True
  57.  
  58.  
  59. #=====================================VARIABLES============================================
  60. SEARCH = StringVar()
  61.  
  62. #=====================================FRAME================================================
  63. Top = Frame(root, width=500, bd=1, relief=SOLID)
  64. Top.pack(side=TOP)
  65. TopFrame = Frame(root, width=500)
  66. TopFrame.pack(side=TOP)
  67. TopForm= Frame(TopFrame, width=300)
  68. TopForm.pack(side=LEFT, pady=10)
  69. TopMargin = Frame(TopFrame, width=260)
  70. TopMargin.pack(side=LEFT)
  71. MidFrame = Frame(root, width=500)
  72. MidFrame.pack(side=TOP)
  73.  
  74. #=====================================ENTRY WIDGET=========================================
  75. vcmd = TopForm.register(onValidate)
  76. search = Entry(TopForm,bd=1,validate="key", textvariable=SEARCH ,  validatecommand=(vcmd,'%P'))
  77. #earch = Entry(TopForm,bd=1,validate="forced", textvariable=SEARCH ,  validatecommand=(vcmd,'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W'))
  78. search.pack(side=LEFT)
  79. search.focus()
  80.  
  81. #=====================================Tree view WIDGET=========================================
  82. scrollbarx = Scrollbar(MidFrame, orient=HORIZONTAL)
  83. scrollbary = Scrollbar(MidFrame, orient=VERTICAL)
  84. tree = ttk.Treeview(MidFrame, columns=("MemberID", "Firstname", "Lastname", "Address", "Age"), selectmode="browse", height=400, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
  85. scrollbary.config(command=tree.yview)
  86. scrollbary.pack(side=RIGHT, fill=Y)
  87. scrollbarx.config(command=tree.xview)
  88. scrollbarx.pack(side=BOTTOM, fill=X)
  89. tree.heading('MemberID', text="MemberID",anchor=W)
  90. tree.heading('Firstname', text="Firstname",anchor=W)
  91. tree.heading('Lastname', text="Lastname",anchor=W)
  92. tree.heading('Address', text="Address",anchor=W)
  93. tree.heading('Age', text="Age",anchor=W)
  94. tree.column('#0', stretch=NO, minwidth=0, width=0)
  95. tree.column('#1', stretch=NO, minwidth=0, width=0)
  96. tree.column('#2', stretch=NO, minwidth=0, width=80)
  97. tree.column('#3', stretch=NO, minwidth=0, width=120)
  98. tree.column('#4', stretch=NO, minwidth=0, width=170)
  99. tree.column('#5', stretch=NO, minwidth=0, width=80)
  100. tree.pack()
  101.  
  102. #=====================================INITIALIZATION=======================================
  103. if __name__ == '__main__':
  104.     Database()
  105.     root.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top