Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. import tkinter as tk
  2. import mysql.connector
  3.  
  4. # Connect to database
  5. database = mysql.connector.connect(
  6. host="localhost",
  7. user="root",
  8. password="YaNa2203",
  9. )
  10.  
  11. # Create Window
  12. window = tk.Tk()
  13. window.title("Service Finder")
  14.  
  15. # Window properties
  16. window.resizable(width=False, height=False)
  17.  
  18. ## Widgets
  19.  
  20.  
  21. # Radio options
  22. radioGP = tk.Checkbutton(window, text="Doctors", state='disabled')
  23. radioGP.grid(row=2, column=0, sticky=tk.W)
  24. radioDentist = tk.Checkbutton(window, text="Dentist", state='disabled')
  25. radioDentist.grid(row=3, column=0, sticky=tk.W)
  26. radioOptician = tk.Checkbutton(window, text="Optician", state='disabled')
  27. radioOptician.grid(row=4, column=0, sticky=tk.W)
  28. radioSchool = tk.Checkbutton(window, text="School", state='disabled')
  29. radioSchool.grid(row=5, column=0, sticky=tk.W)
  30. radioNursery = tk.Checkbutton(window, text="Nursery", state='disabled')
  31. radioNursery.grid(row=6, column=0, sticky=tk.W)
  32.  
  33. # Search
  34. def loadDatabase():
  35. # Get script
  36. fd = open('Find Distance.sql', 'r')
  37. sqlFile = fd.read()
  38. fd.close()
  39.  
  40. cursor = database.cursor(buffered=True)
  41.  
  42. # Constants to replace in the script
  43. postcodeconst = '$POSTCODE$'
  44. categoryconst = '$CATEGORY$'
  45.  
  46. # Set the correct database
  47. database.cursor().execute("USE project;")
  48.  
  49. text = postcodeField.get()
  50. text = text.replace(" ", "")
  51. text = text[:4] + " " + text[4:]
  52. print(text)
  53. # Replace the constants in the script
  54. sqlFile = sqlFile.replace(postcodeconst, text)
  55. sqlFile = sqlFile.replace(categoryconst, str(1))
  56.  
  57. # Execute the script
  58. result = cursor.execute(sqlFile)
  59.  
  60. # Clear list
  61. listbox1.delete(0,tk.END)
  62. listbox2.delete(0,tk.END)
  63. listbox3.delete(0,tk.END)
  64. listbox4.delete(0,tk.END)
  65.  
  66. # Insert database into list
  67. for x in cursor:
  68. listbox1.insert(tk.END, x[0])
  69. listbox2.insert(tk.END, x[1])
  70. listbox3.insert(tk.END, x[2])
  71. listbox4.insert(tk.END, str(x[3]) + " miles")
  72.  
  73. noticeText = tk.StringVar()
  74. noticeLabel = tk.Label(window, textvariable=noticeText)
  75. noticeLabel.grid(row=7, column=0, sticky=tk.W)
  76. noticeText.set("* required field")
  77. # Search button
  78. searchButton = tk.Button(text="Search", command=loadDatabase, state='disabled')
  79. searchButton.grid(row=8, column =0, columnspan=3, sticky=tk.W)
  80.  
  81. # Postcode
  82. postcodeText = tk.StringVar()
  83. postcodeLabel = tk.Label(window, textvariable=postcodeText)
  84. postcodeLabel.grid(row=0, column=0, sticky=tk.W)
  85. postcodeText.set("Postcode *:")
  86.  
  87. # When the postcode text changes
  88. def postChange(text):
  89. text = text.replace(" ", "")
  90. if len(text) != 7:
  91. searchButton.config(state='disabled')
  92. else:
  93. searchButton.config(state='normal')
  94.  
  95. # Postcode Button
  96. postcodeFieldText = tk.StringVar()
  97. postcodeFieldText.trace("w", lambda name, index, mode, postcodeFieldText=postcodeFieldText: postChange(postcodeFieldText.get()))
  98. postcodeField = tk.Entry(window, bd = 5, textvariable=postcodeFieldText)
  99. postcodeField.grid(row=0, column=1, sticky=tk.W)
  100.  
  101. # On age change
  102. def ageChange(text):
  103. # Check the age, and enable the appropriate buttons
  104. if text != "":
  105. radioGP.config(state='normal')
  106. radioDentist.config(state='normal')
  107. radioOptician.config(state='normal')
  108. if int(text) < 4:
  109. radioNursery.config(state='normal')
  110. radioSchool.config(state='normal')
  111. radioSchool.config(state='disabled')
  112. elif int(text) <= 16:
  113. radioNursery.config(state='disabled')
  114. radioSchool.config(state='normal')
  115. else:
  116. radioSchool.config(state='disabled')
  117. radioNursery.config(state='disabled')
  118. var.set(99)
  119. else:
  120. radioSchool.config(state='disabled')
  121. radioNursery.config(state='disabled')
  122. radioGP.config(state='disabled')
  123. radioDentist.config(state='disabled')
  124. radioOptician.config(state='disabled')
  125. var.set(99)
  126.  
  127. ageText = tk.StringVar()
  128. ageLabel = tk.Label(window, textvariable=ageText)
  129. ageLabel.grid(row=1, column=0, sticky=tk.W)
  130. ageText.set("Age *:")
  131.  
  132. ageFieldText = tk.StringVar()
  133. ageFieldText.trace("w", lambda name, index, mode, ageFieldText=ageFieldText: ageChange(ageFieldText.get()))
  134. ageField = tk.Entry(window, bd = 5, textvariable=ageFieldText)
  135. ageField.grid(row=1, column=1, sticky=tk.W)
  136.  
  137. # Canvas for the listboxes
  138. frame = tk.Frame(window, width=400, height=400)
  139. frame.grid(row=0, column=3, rowspan=20)
  140.  
  141. canvas = tk.Canvas(frame, width=400, height=400, scrollregion=(0,0,500,500), confine=False)
  142.  
  143. canvas.pack(side=tk.LEFT,expand=True,fill=tk.BOTH)
  144.  
  145. # SQL table
  146. listbox1 = tk.Listbox(canvas, height=32)
  147. listbox1.grid(column=0, row = 0)
  148. listbox2 = tk.Listbox(canvas, height=32)
  149. listbox2.grid(column=1, row = 0)
  150. listbox3 = tk.Listbox(canvas, height=32)
  151. listbox3.grid(column=2, row = 0)
  152. listbox4 = tk.Listbox(canvas, height=32)
  153. listbox4.grid(column=3, row = 0)
  154.  
  155. # Select the corresponding box on click
  156. def lb1Select(e):
  157. if len(listbox1.curselection()):
  158. listbox2.select_set(listbox1.curselection()[0])
  159. listbox1.bind('<<ListboxSelect>>', lb1Select)
  160.  
  161. # Start window
  162. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement