Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ---------------------connect with class tkinter
- try:
- # for Python2
- from Tkinter import * ## notice capitalized T in Tkinter
- except ImportError:
- # for Python3
- from tkinter import * ## notice lowercase 't' in tkinter here
- #---------------------method for make windows program
- root = Tk()
- #--------------------element that will be inside level
- w = Label(root, text="Hello World")
- #--------------------for display lebel
- w.pack()
- #--------------------for execute everything
- root.mainloop()
- ____________________________________________________________________________________
- Creating frame & button
- try:
- # for Python2
- from Tkinter import * ## notice capitalized T in Tkinter
- except ImportError:
- # for Python3
- from tkinter import * ## notice lowercase 't' in tkinter here
- root = Tk()
- #---- creating frame
- frame = Frame(root)
- #---- diplay frame
- frame.pack()
- #---- creating button
- button = Button(frame,text = "click me",fg = "green")
- #---- diplay frame , you can initailize side
- button.pack(side="left")
- root.mainloop()
- __________________________________________________________________________
- Text Field & grid create
- #--- level for text field
- name_lebel = Label(root, text="write your name")
- password_lebel = Label(root, text="write your password")
- #--- text field create
- text_field = Entry(root)
- password_field = Entry(root)
- check_button = Checkbutton(root,text="Keep me log in")
- #--- creating grid for fields
- name_lebel.grid(row = 0)
- text_field.grid(row = 0, column = 1)
- password_lebel.grid(row = 1)
- password_field.grid(row = 1, column = 1)
- check_button.grid(row = 2, column = 0)
- root.mainloop()
- _______________________________________________________________________________
- Onclick function :
- # function for onclick event
- def ClickmeFunction(event):
- print("thanks for click me")
- # creating button
- button = Button(root,text = "click me")
- # bind event with button, here <Button-1> means left click mouse | <Button-2> middleclick | <Button-3> right click
- button.bind("<Button-1>",ClickmeFunction)
- button.pack()
- ____________________________________________________________________________________________
- Create Menu :
- #creating mainmenu in root
- mainmenu = Menu(root)
- #output menu
- root.config(menu=mainmenu)
- #creating submenu under mainmenu
- submenu = Menu(mainmenu)
- #set submenu title & link with menu
- submenu.add_cascade(label="change password")
- #output menu
- #set menu title link with submenu | if no submenu then no menu required
- mainmenu.add_cascade(label="setting",menu=submenu)
- root.mainloop()
- _________________________________________________________
- save & open :
- open by : IDLE (Python 3.7 64-bit)
- save anywhere .py name
- _________________________________________________________
- calculator :
- # text field create
- num1 = Entry(root)
- num1.pack()
- num2 = Entry(root)
- num2.pack()
- output = Entry(root)
- output.pack()
- # function create & get data from field & publish in output place
- def calculation_function(event):
- get_value1 = num1.get()
- get_value2 = num2.get()
- result = get_value1 + get_value2
- output.insert(0,result)
- # create equal button & function
- button = Button(root,text = "=")
- button.bind("<Button-1>",calculation_function)
- button.pack()
- root.mainloop()
- _____________________________________________________________________________________
- Database connection :
- first download package by windows cmd :
- cd C:\Users\arman\AppData\Local\Programs\Python\Python37\Scripts
- then
- pip install mysql (download mysql)
- check by open idle python cmd type : import pymysql
- code :
- import pymysql
- # Open database connection
- db = pymysql.connect("localhost","root","","binary" )
- # prepare a cursor object using cursor() method
- cursor = db.cursor()
- # disconnect from server
- db.close()
- ______________________________________________________________________________
- Insert :
- import pymysql
- # Open database connection
- db = pymysql.connect("localhost","root","","binary" )
- # prepare a cursor object using cursor() method
- cursor = db.cursor()
- data = 2.1
- # Create table as per requirement
- sql = "INSERT INTO employee(FIRST_NAME,LAST_NAME,AGE,SEX,INCOME) VALUES ('1','1','1','1','1')"
- try:
- # Execute the SQL command
- cursor.execute(sql)
- # Commit your changes in the database
- db.commit()
- except:
- # Rollback in case there is any error
- db.rollback()
- # disconnect from server
- db.close()
- View :
- sql = "SELECT * FROM employee"
- try:
- # Execute the SQL command
- cursor.execute(sql)
- # Fetch all the rows in a list of lists.
- results = cursor.fetchall()
- for row in results:
- # Now print fetched result
- fname = row[0]
- lname = row[1]
- age = row[2]
- sex = row[3]
- income = row[4]
- print("fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
- (fname, lname, age, sex, income ))
- except:
- print("Error: unable to fecth data")
Add Comment
Please, Sign In to add comment