Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Tkinter import *
- from tkinter.ttk import *
- import mysql.connector
- class TKGUI:
- def __init__(self):
- self.root= Tk() ##initilise the main window
- self.quit = False ##defaults the quit flag
- self.name = None
- self.age = None
- self.mnumber = None
- self.email = None
- self.root = None
- self.frame = None
- self.warninglabel = None
- self.s = None
- self.warninglabel = None
- def submit(self):
- getName = self.name.get()
- getAge = self.age.get()
- getMobileNumber = self.mnumber.get()
- getEmail = self.email.get()
- if getName == "" or getAge == "" or getEmail == "" or getMobileNumber == "":
- self.s.configure('warning.TLabel',foreground="red")
- if getEmail == "":
- self.warninglabel.config(text="Please Enter The Email Address")
- if getMobileNumber == "":
- self.warninglabel.config(text="Please Enter the Mobile Number")
- if getAge == "":
- self.warninglabel.config(text="Please Enter the Age")
- if getName == "":
- self.warninglabel.config(text="Please Enter the Name")
- if getName == "" and getAge =="" and getEmail == "" and getMobileNumber == "":
- self.warninglabel.config(text="Please Fill the Form")
- else:
- sql = "INSERT INTO form(Name,Age,Mobile,Email) VALUES(%s, %s,%s, %s)"
- values = (getName,getAge,getMobileNumber,getEmail)
- mycursor.execute(sql,values)
- mydb.commit()
- self.name.delete(0,'end')
- self.age.delete(0,'end')
- self.email.delete(0,'end')
- self.mnumber.delete(0,'end')
- self.warninglabel.config(text="User Registererd!")
- self.s.configure('warning.TLabel',foreground="green")
- def run(self): ##main part of the application
- self.root.configure(bg="white") #sets the background to white rather than default gray.
- self.root.protocol("WM_DELETE_WINDOW", self.quitting) ##changes the X (close) Button to run a function instead.
- try:
- self.root.iconbitmap("fav.ico") ##sets the application Icon
- except:
- pass
- self.root.title("MyApp")
- self.root.geometry("800x600")
- root.title("REGISTERATION")
- root.minsize(width=270,height=350)
- root.maxsize(width=270,height=350)
- s = Style()
- s.configure('TFrame',background="#DDDDDD")
- s.configure('TLabel',background="#DDDDDD",font=("arial",10,"bold"))
- s.configure('warning.TLabel',background="white",foreground="Black",font=("arial",10,"bold"))
- s.configure('new.TLabel',foreground="#646464",relief=GROOVE,anchor="center")
- self.frame = Frame(root,relief=GROOVE)
- self.frame.pack(side="top",anchor="w",padx=10,pady=10,ipadx=10,ipady=10)
- #main registeration form heading
- label = Label(self.frame,text="User Registeration",font=("impact",20),style="new.TLabel")
- label.pack(padx=10,pady=5,ipadx=5,ipady=5)
- #main form lables and entry box
- #name entry
- label = Label(self.frame,text="Enter Your Name:")
- label.pack(anchor="w",padx=10)
- name = Entry(self.frame)
- name.pack(anchor="w",padx=10)
- #age entry
- label = Label(self.frame,text="Enter Your Age:")
- label.pack(anchor="w",padx=10)
- age = Entry(self.frame)
- age.pack(anchor="w",padx=10)
- #mobile number entry
- label = Label(self.frame,text="Mobile Number:")
- label.pack(anchor="w",padx=10)
- mnumber = Entry(self.frame,show="*")
- mnumber.pack(anchor="w",padx=10)
- #Email address entry
- label = Label(self.frame,text="Email Address:")
- label.pack(anchor="w",padx=10)
- email = Entry(self.frame)
- email.pack(anchor="w",padx=10)
- #submit button
- btn = Button(self.frame, text="Submit",command=self.submit)
- btn.pack(anchor="w",padx=10,pady=5)
- #warning
- self.warninglabel = Label(self.frame,text="Please Enter the Data in the above",style="warning.TLabel",anchor="n")
- self.warninglabel.pack(fill=X)
- developer = Label(root,text="Developed By: Jawad Saqib")
- developer.pack(side="left")
- #############################################################
- while not self.quit: ##flag to quit the application
- self.root.update_idletasks() #updates the root. same as root.mainloop() but safer and interruptable
- self.root.update() #same as above. This lest you stop the loop or add things to the loop.
- #add extra functions here if you need them to run with the loop#
- def quitting(self): ##to set the quit flag
- self.quit = True
- if __name__ == "__main__":
- app = TKGUI() ##creates instance of GUI class
- try:
- app.run()# starts the application
- except KeyboardInterrupt:
- app.quitting() ##safely quits the application when crtl+C is pressed
- except:
- raise #you can change this to be your own error handler if needed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement