danyalbasar

sms_pyre

Aug 4th, 2021 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.24 KB | None | 0 0
  1. from pyrebase import *
  2. from tkinter import *
  3. from tkinter.messagebox import *
  4. from tkinter.scrolledtext import *
  5. import matplotlib.pyplot as plt
  6. import requests
  7. import bs4
  8.  
  9. firebaseConfig = {
  10.     }
  11.  
  12. firebase = initialize_app(firebaseConfig)
  13. db = firebase.database()
  14.  
  15. def f1():
  16.     add_window.deiconify()
  17.     main_window.withdraw()
  18. def f2():
  19.     main_window.deiconify()
  20.     add_window.withdraw()
  21.  
  22. def add():
  23.     info = {}
  24.     try:
  25.         rno = aw_ent_rno.get()
  26.         name = aw_ent_name.get()
  27.         marks = aw_ent_marks.get()
  28.         if (not rno.isdigit()) or (len(rno) == 0) or (int(rno) <= 0):
  29.             showerror("Roll No Issue", "Invalid roll no")      
  30.             aw_ent_rno.delete(0, END)
  31.             aw_ent_rno.focus()
  32.             return
  33.         if (not name.isalpha()) or (len(name) < 2):
  34.             showerror("Name Issue", "Invalid name")
  35.             aw_ent_name.delete(0, END)
  36.             aw_ent_name.focus()
  37.             return
  38.         if (not marks.isdigit()) or (len(marks) == 0) or (int(marks) < 0) or (int(marks) > 100):
  39.             showerror("Marks Issue", "Invalid marks")
  40.             aw_ent_marks.delete(0, END)
  41.             aw_ent_marks.focus()
  42.             return
  43.         data = db.child("student").get()
  44.         if data.pyres is not None and rno in data.val():
  45.             showerror("Error", "Roll No Already Exists")
  46.         else:
  47.             info = {"rno": int(rno), "name": name, "marks": int(marks)}
  48.             db.child("student").child(rno).set(info)
  49.             showinfo("Success", "Record created")
  50.        
  51.         aw_ent_rno.delete(0, END)
  52.         aw_ent_name.delete(0, END)
  53.         aw_ent_marks.delete(0, END)
  54.         aw_ent_rno.focus()
  55.     except Exception as e:
  56.         showerror("Error", str(e))
  57. def view():
  58.     view_window.deiconify()
  59.     main_window.withdraw()     
  60.     vw_st_data.delete(1.0, END)
  61.     info = {}
  62.     try:
  63.         data = db.child("student").get()
  64.         #print(data.val())
  65.         if data.pyres:
  66.             for d in data.each():
  67.                 info = str(d.val()) + "\n"
  68.                 vw_st_data.insert(INSERT, info)
  69.         else:
  70.             showerror("Error", "No data")
  71.     except Exception as e:
  72.         showerror("Error", str(e))
  73. def f4():
  74.     main_window.deiconify()
  75.     view_window.withdraw()
  76. def f5():
  77.     update_window.deiconify()
  78.     main_window.withdraw()
  79. def f6():
  80.     main_window.deiconify()
  81.     update_window.withdraw()
  82. def f7():
  83.     delete_window.deiconify()
  84.     main_window.withdraw()
  85. def f8():
  86.     main_window.deiconify()
  87.     delete_window.withdraw()
  88. def charts():
  89.     info = {}
  90.     try:
  91.         data = db.child("student").get()
  92.         name = ""
  93.         marks = int()
  94.         name_list = []
  95.         marks_list = []
  96.         if data.pyres:
  97.             for d in data.each():
  98.                 info = d.val()
  99.                 name = info.get("name")
  100.                 marks = info.get("marks")
  101.                 name_list.append(name)
  102.                 marks_list.append(marks)
  103.         else:
  104.             showerror("Error", "No data")
  105.  
  106.         plt.bar(name_list, marks_list, width=0.7, color=["red", "green", "blue"])
  107.         plt.ylim(0, 105)
  108.         plt.xlabel("Students")
  109.         plt.ylabel("Marks")
  110.         plt.title("Batch Information")
  111.         plt.show()
  112.  
  113.     except Exception as e:
  114.         showerror("Error", str(e))
  115. def update():
  116.     info = {}
  117.     try:
  118.         rno = uw_ent_rno.get()
  119.         name = uw_ent_name.get()
  120.         marks = uw_ent_marks.get()
  121.         if (not rno.isdigit()) or (len(rno) == 0) or (int(rno) <= 0):
  122.             showerror("Roll No Issue", "Invalid roll no")      
  123.             uw_ent_rno.delete(0, END)
  124.             uw_ent_rno.focus()
  125.             return
  126.         if (not name.isalpha()) or (len(name) < 2):
  127.             showerror("Name Issue", "Invalid name")
  128.             uw_ent_name.delete(0, END)
  129.             uw_ent_name.focus()
  130.             return
  131.         if (not marks.isdigit()) or (len(marks) == 0) or (int(marks) < 0) or (int(marks) > 100):
  132.             showerror("Marks Issue", "Invalid marks")
  133.             uw_ent_marks.delete(0, END)
  134.             uw_ent_marks.focus()
  135.             return
  136.         data = db.child("student").get()
  137.         if data.pyres is not None and rno in data.val() :
  138.             info = {"rno": int(rno), "name": name, "marks": int(marks)}
  139.             db.child("student").child(rno).update(info)
  140.             showinfo("Success", "Record updated")
  141.         else:
  142.             showerror("Error","Record not found")
  143.  
  144.         uw_ent_rno.delete(0, END)
  145.         uw_ent_name.delete(0, END)
  146.         uw_ent_marks.delete(0, END)
  147.         uw_ent_rno.focus()
  148.     except Exception as e:
  149.         showerror("Error", str(e))
  150. def delete():
  151.     try:
  152.         rno = dw_ent_rno.get()
  153.         if (not rno.isdigit()) or (len(rno) == 0) or (int(rno) <= 0):
  154.             showerror("Roll No Issue", "Invalid roll no")      
  155.             dw_ent_rno.delete(0, END)
  156.             dw_ent_rno.focus()
  157.             return
  158.         data = db.child("student").get()
  159.         if data.pyres is not None and rno in data.val():
  160.             db.child("student").child(rno).remove()
  161.             showinfo("Success","Record deleted")
  162.         else:
  163.             showerror("Error","Record not found")
  164.         dw_ent_rno.delete(0, END)
  165.         dw_ent_rno.focus()
  166.     except Exception as e:
  167.         showerror("Error", str(e))
  168.  
  169. main_window = Tk()
  170. main_window.title("S. M. S")
  171. main_window.geometry("500x530+650+225")
  172. main_window.configure(bg="antique white")
  173. main_window.resizable(False, False)
  174.  
  175. f = ("Arial", 18, "bold")
  176.  
  177. mw_btn_add = Button(main_window, text="Add", bd=3.5, font=f, width=10, command=f1)
  178. mw_btn_add.pack(pady=8)
  179. mw_btn_view = Button(main_window, text="View", bd=3.5, font=f, width=10, command=view)
  180. mw_btn_view.pack(pady=8)
  181. mw_btn_update = Button(main_window, text="Update", bd=3.5, font=f, width=10, command=f5)
  182. mw_btn_update.pack(pady=8)
  183. mw_btn_delete = Button(main_window, text="Delete", bd=3.5, font=f, width=10, command=f7)
  184. mw_btn_delete.pack(pady=8)
  185. mw_btn_charts = Button(main_window, text="Charts", bd=3.5, font=f, width=10, command=charts)
  186. mw_btn_charts.pack(pady=8)
  187.  
  188. add_window = Toplevel(main_window, bg="antique white")
  189. add_window.title("Add Stu.")
  190. add_window.geometry("500x530+650+225")
  191. add_window.resizable(False, False)
  192.  
  193. aw_lbl_rno = Label(add_window, text="Enter Roll No:", font=f, bg="antique white")
  194. aw_ent_rno = Entry(add_window, bd=5, width=29, font=f)
  195. aw_lbl_name = Label(add_window, text="Enter Name:", font=f, bg="antique white")
  196. aw_ent_name = Entry(add_window, bd=5, width=29, font=f)
  197. aw_lbl_marks = Label(add_window, text="Enter Marks:", font=f, bg="antique white")
  198. aw_ent_marks = Entry(add_window, width=29, bd=5, font=f)
  199. aw_btn_save = Button(add_window, text=" Save ", width=10, bd=3.5, font=f, command=add)
  200. aw_btn_back = Button(add_window, text="Back", width=10, bd=3.5, font=f, command=f2)
  201. aw_lbl_rno.pack(pady=8)
  202. aw_ent_rno.pack(pady=8)
  203. aw_lbl_name.pack(pady=8)
  204. aw_ent_name.pack(pady=8)
  205. aw_lbl_marks.pack(pady=8)
  206. aw_ent_marks.pack(pady=8)
  207. aw_btn_save.pack(pady=12)
  208. aw_btn_back.pack(pady=3)
  209.  
  210. add_window.withdraw()
  211.  
  212. view_window = Toplevel(main_window, bg="antique white")
  213. view_window.title("View Stu.")
  214. view_window.geometry("500x530+650+225")
  215. view_window.resizable(False, False)
  216.  
  217. vw_st_data = ScrolledText(view_window, width=33, height=12, font=f, bg="white")
  218. vw_btn_back = Button(view_window, text="Back", width=10, bd=3.5, font=f, command=f4)
  219. vw_st_data.pack(pady=20)
  220. vw_btn_back.pack(pady=8)
  221.  
  222. view_window.withdraw()
  223.  
  224. update_window = Toplevel(main_window, bg="antique white")
  225. update_window.title("Update Stu.")
  226. update_window.geometry("500x530+650+225")
  227. update_window.resizable(False, False)
  228.  
  229. uw_lbl_rno = Label(update_window, text="Enter Roll No:", font=f, bg="antique white")
  230. uw_ent_rno = Entry(update_window, width=29, bd=5, font=f)
  231. uw_lbl_name = Label(update_window, text="Enter Name:", font=f, bg="antique white")
  232. uw_ent_name = Entry(update_window, width=29, bd=5, font=f)
  233. uw_lbl_marks = Label(update_window, text="Enter Marks:", font=f, bg="antique white")
  234. uw_ent_marks = Entry(update_window, width=29, bd=5, font=f)
  235. uw_btn_update = Button(update_window, text="Update", width=10, bd=3.5, font=f, command=update)
  236. uw_btn_back = Button(update_window, text="Back", width=10, bd=3.5, font=f, command=f6)
  237. uw_lbl_rno.pack(pady=8)
  238. uw_ent_rno.pack(pady=8)
  239. uw_lbl_name.pack(pady=8)
  240. uw_ent_name.pack(pady=8)
  241. uw_lbl_marks.pack(pady=8)
  242. uw_ent_marks.pack(pady=8)
  243. uw_btn_update.pack(pady=12)
  244. uw_btn_back.pack(pady=3)
  245.  
  246. update_window.withdraw()
  247.  
  248. delete_window = Toplevel(main_window, bg="antique white")
  249. delete_window.title("Delete Stu.")
  250. delete_window.geometry("500x530+650+225")
  251. delete_window.resizable(False, False)
  252.  
  253. dw_lbl_rno = Label(delete_window, text="Enter Roll No:", font=f, bg="antique white")
  254. dw_ent_rno = Entry(delete_window, width=29, bd=5, font=f)
  255. dw_btn_delete = Button(delete_window, text="Delete", width=10, bd=3.5, font=f, command=delete)
  256. dw_btn_back = Button(delete_window, text="Back", width=10, bd=3.5, font=f, command=f8)
  257. dw_lbl_rno.pack(pady=8)
  258. dw_ent_rno.pack(pady=8)
  259. dw_btn_delete.pack(pady=12)
  260. dw_btn_back.pack(pady=3)
  261.  
  262. delete_window.withdraw()
  263.  
  264. main_window.mainloop()
Add Comment
Please, Sign In to add comment