Advertisement
AceScottie

app-class.py

May 9th, 2019
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.52 KB | None | 0 0
  1. from Tkinter import *
  2. from tkinter.ttk import *
  3. import mysql.connector
  4.  
  5. class TKGUI:
  6.     def __init__(self):
  7.         self.root= Tk() ##initilise the main window
  8.         self.quit = False ##defaults the quit flag
  9.         self.name = None
  10.         self.age = None
  11.         self.mnumber = None
  12.         self.email = None
  13.         self.root = None
  14.         self.frame = None
  15.         self.warninglabel = None
  16.         self.s = None
  17.         self.warninglabel = None
  18.        
  19.        
  20.     def submit(self):
  21.         getName = self.name.get()
  22.         getAge = self.age.get()
  23.         getMobileNumber = self.mnumber.get()
  24.         getEmail = self.email.get()
  25.      
  26.         if getName == "" or getAge == "" or getEmail == "" or getMobileNumber == "":
  27.             self.s.configure('warning.TLabel',foreground="red")
  28.             if getEmail == "":
  29.                 self.warninglabel.config(text="Please Enter The Email Address")
  30.             if getMobileNumber == "":
  31.                 self.warninglabel.config(text="Please Enter the Mobile Number")
  32.             if getAge == "":
  33.                 self.warninglabel.config(text="Please Enter the Age")
  34.             if getName == "":
  35.                 self.warninglabel.config(text="Please Enter the Name")
  36.             if getName == "" and getAge =="" and getEmail == "" and getMobileNumber == "":
  37.                 self.warninglabel.config(text="Please Fill the Form")
  38.      
  39.         else:
  40.             sql = "INSERT INTO form(Name,Age,Mobile,Email) VALUES(%s, %s,%s, %s)"
  41.             values = (getName,getAge,getMobileNumber,getEmail)
  42.             mycursor.execute(sql,values)
  43.             mydb.commit()
  44.             self.name.delete(0,'end')
  45.             self.age.delete(0,'end')
  46.             self.email.delete(0,'end')
  47.             self.mnumber.delete(0,'end')
  48.             self.warninglabel.config(text="User Registererd!")
  49.             self.s.configure('warning.TLabel',foreground="green")
  50.  
  51.     def run(self): ##main part of the application
  52.         self.root.configure(bg="white") #sets the background to white rather than default gray.
  53.         self.root.protocol("WM_DELETE_WINDOW", self.quitting) ##changes the X (close) Button to run a function instead.
  54.         try:
  55.             self.root.iconbitmap("fav.ico") ##sets the application Icon
  56.         except:
  57.             pass
  58.         self.root.title("MyApp")
  59.         self.root.geometry("800x600")
  60.  
  61.        
  62.        
  63.         root.title("REGISTERATION")
  64.         root.minsize(width=270,height=350)
  65.         root.maxsize(width=270,height=350)
  66.      
  67.         s = Style()
  68.         s.configure('TFrame',background="#DDDDDD")
  69.         s.configure('TLabel',background="#DDDDDD",font=("arial",10,"bold"))
  70.         s.configure('warning.TLabel',background="white",foreground="Black",font=("arial",10,"bold"))
  71.         s.configure('new.TLabel',foreground="#646464",relief=GROOVE,anchor="center")
  72.         self.frame = Frame(root,relief=GROOVE)
  73.         self.frame.pack(side="top",anchor="w",padx=10,pady=10,ipadx=10,ipady=10)
  74.      
  75.         #main registeration form heading
  76.         label = Label(self.frame,text="User Registeration",font=("impact",20),style="new.TLabel")
  77.         label.pack(padx=10,pady=5,ipadx=5,ipady=5)
  78.         #main form lables and entry box
  79.         #name entry
  80.         label = Label(self.frame,text="Enter Your Name:")
  81.         label.pack(anchor="w",padx=10)
  82.         name = Entry(self.frame)
  83.         name.pack(anchor="w",padx=10)
  84.         #age entry
  85.         label = Label(self.frame,text="Enter Your Age:")
  86.         label.pack(anchor="w",padx=10)
  87.         age = Entry(self.frame)
  88.         age.pack(anchor="w",padx=10)
  89.         #mobile number entry
  90.         label = Label(self.frame,text="Mobile Number:")
  91.         label.pack(anchor="w",padx=10)
  92.         mnumber = Entry(self.frame,show="*")
  93.         mnumber.pack(anchor="w",padx=10)
  94.         #Email address entry
  95.         label = Label(self.frame,text="Email Address:")
  96.         label.pack(anchor="w",padx=10)
  97.         email = Entry(self.frame)
  98.         email.pack(anchor="w",padx=10)
  99.         #submit button
  100.         btn = Button(self.frame, text="Submit",command=self.submit)
  101.         btn.pack(anchor="w",padx=10,pady=5)
  102.         #warning
  103.         self.warninglabel = Label(self.frame,text="Please Enter the Data in the above",style="warning.TLabel",anchor="n")
  104.         self.warninglabel.pack(fill=X)
  105.      
  106.         developer = Label(root,text="Developed By: Jawad Saqib")
  107.         developer.pack(side="left")
  108.        
  109.        
  110.        
  111.         #############################################################
  112.         while not self.quit: ##flag to quit the application
  113.             self.root.update_idletasks() #updates the root. same as root.mainloop() but safer and interruptable
  114.             self.root.update() #same as above. This lest you stop the loop or add things to the loop.
  115.             #add extra functions here if you need them to run with the loop#
  116.  
  117.     def quitting(self): ##to set the quit flag
  118.         self.quit = True
  119.            
  120. if __name__ == "__main__":
  121.     app = TKGUI() ##creates instance of GUI class
  122.     try:
  123.         app.run()# starts the application
  124.     except KeyboardInterrupt:
  125.         app.quitting() ##safely quits the application when crtl+C is pressed
  126.     except:
  127.         raise #you can change this to be your own error handler if needed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement