Guest User

Untitled

a guest
Apr 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. # ---------------------connect with class tkinter
  2.  
  3. try:
  4. # for Python2
  5. from Tkinter import * ## notice capitalized T in Tkinter
  6. except ImportError:
  7. # for Python3
  8. from tkinter import * ## notice lowercase 't' in tkinter here
  9.  
  10. #---------------------method for make windows program
  11.  
  12. root = Tk()
  13.  
  14. #--------------------element that will be inside level
  15.  
  16. w = Label(root, text="Hello World")
  17.  
  18. #--------------------for display lebel
  19.  
  20. w.pack()
  21.  
  22. #--------------------for execute everything
  23.  
  24. root.mainloop()
  25.  
  26.  
  27. ____________________________________________________________________________________
  28.  
  29. Creating frame & button
  30.  
  31.  
  32. try:
  33. # for Python2
  34. from Tkinter import * ## notice capitalized T in Tkinter
  35. except ImportError:
  36. # for Python3
  37. from tkinter import * ## notice lowercase 't' in tkinter here
  38.  
  39.  
  40. root = Tk()
  41.  
  42. #---- creating frame
  43. frame = Frame(root)
  44.  
  45. #---- diplay frame
  46. frame.pack()
  47.  
  48. #---- creating button
  49. button = Button(frame,text = "click me",fg = "green")
  50.  
  51. #---- diplay frame , you can initailize side
  52. button.pack(side="left")
  53.  
  54.  
  55. root.mainloop()
  56.  
  57.  
  58. __________________________________________________________________________
  59.  
  60. Text Field & grid create
  61.  
  62.  
  63.  
  64.  
  65. #--- level for text field
  66.  
  67. name_lebel = Label(root, text="write your name")
  68. password_lebel = Label(root, text="write your password")
  69.  
  70. #--- text field create
  71.  
  72. text_field = Entry(root)
  73. password_field = Entry(root)
  74. check_button = Checkbutton(root,text="Keep me log in")
  75.  
  76. #--- creating grid for fields
  77.  
  78. name_lebel.grid(row = 0)
  79. text_field.grid(row = 0, column = 1)
  80. password_lebel.grid(row = 1)
  81. password_field.grid(row = 1, column = 1)
  82. check_button.grid(row = 2, column = 0)
  83.  
  84. root.mainloop()
  85.  
  86.  
  87.  
  88. _______________________________________________________________________________
  89.  
  90.  
  91. Onclick function :
  92.  
  93. # function for onclick event
  94.  
  95. def ClickmeFunction(event):
  96. print("thanks for click me")
  97.  
  98. # creating button
  99.  
  100. button = Button(root,text = "click me")
  101.  
  102.  
  103. # bind event with button, here <Button-1> means left click mouse | <Button-2> middleclick | <Button-3> right click
  104.  
  105. button.bind("<Button-1>",ClickmeFunction)
  106.  
  107. button.pack()
  108.  
  109.  
  110.  
  111.  
  112. ____________________________________________________________________________________________
  113.  
  114.  
  115. Create Menu :
  116.  
  117.  
  118. #creating mainmenu in root
  119.  
  120. mainmenu = Menu(root)
  121.  
  122. #output menu
  123.  
  124. root.config(menu=mainmenu)
  125.  
  126.  
  127. #creating submenu under mainmenu
  128.  
  129. submenu = Menu(mainmenu)
  130.  
  131. #set submenu title & link with menu
  132.  
  133. submenu.add_cascade(label="change password")
  134.  
  135. #output menu
  136.  
  137. #set menu title link with submenu | if no submenu then no menu required
  138.  
  139. mainmenu.add_cascade(label="setting",menu=submenu)
  140.  
  141. root.mainloop()
  142.  
  143.  
  144.  
  145.  
  146. _________________________________________________________
  147.  
  148.  
  149. save & open :
  150.  
  151. open by : IDLE (Python 3.7 64-bit)
  152.  
  153. save anywhere .py name
  154.  
  155.  
  156.  
  157. _________________________________________________________
  158.  
  159.  
  160. calculator :
  161.  
  162.  
  163.  
  164. # text field create
  165.  
  166. num1 = Entry(root)
  167. num1.pack()
  168.  
  169. num2 = Entry(root)
  170. num2.pack()
  171.  
  172. output = Entry(root)
  173. output.pack()
  174.  
  175.  
  176.  
  177. # function create & get data from field & publish in output place
  178.  
  179.  
  180. def calculation_function(event):
  181.  
  182. get_value1 = num1.get()
  183.  
  184. get_value2 = num2.get()
  185.  
  186. result = get_value1 + get_value2
  187.  
  188. output.insert(0,result)
  189.  
  190.  
  191. # create equal button & function
  192.  
  193.  
  194. button = Button(root,text = "=")
  195.  
  196. button.bind("<Button-1>",calculation_function)
  197.  
  198. button.pack()
  199.  
  200.  
  201.  
  202. root.mainloop()
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212. _____________________________________________________________________________________
  213.  
  214.  
  215. Database connection :
  216.  
  217. first download package by windows cmd :
  218.  
  219. cd C:\Users\arman\AppData\Local\Programs\Python\Python37\Scripts
  220.  
  221. then
  222.  
  223. pip install mysql (download mysql)
  224.  
  225. check by open idle python cmd type : import pymysql
  226.  
  227.  
  228. code :
  229.  
  230. import pymysql
  231.  
  232. # Open database connection
  233. db = pymysql.connect("localhost","root","","binary" )
  234.  
  235. # prepare a cursor object using cursor() method
  236. cursor = db.cursor()
  237.  
  238. # disconnect from server
  239. db.close()
  240.  
  241.  
  242. ______________________________________________________________________________
  243.  
  244.  
  245. Insert :
  246.  
  247.  
  248. import pymysql
  249.  
  250. # Open database connection
  251. db = pymysql.connect("localhost","root","","binary" )
  252.  
  253. # prepare a cursor object using cursor() method
  254. cursor = db.cursor()
  255.  
  256. data = 2.1
  257.  
  258. # Create table as per requirement
  259. sql = "INSERT INTO employee(FIRST_NAME,LAST_NAME,AGE,SEX,INCOME) VALUES ('1','1','1','1','1')"
  260.  
  261. try:
  262. # Execute the SQL command
  263. cursor.execute(sql)
  264. # Commit your changes in the database
  265. db.commit()
  266. except:
  267. # Rollback in case there is any error
  268. db.rollback()
  269.  
  270.  
  271. # disconnect from server
  272. db.close()
  273.  
  274.  
  275.  
  276. View :
  277.  
  278.  
  279. sql = "SELECT * FROM employee"
  280.  
  281. try:
  282. # Execute the SQL command
  283. cursor.execute(sql)
  284. # Fetch all the rows in a list of lists.
  285. results = cursor.fetchall()
  286. for row in results:
  287. # Now print fetched result
  288. fname = row[0]
  289. lname = row[1]
  290. age = row[2]
  291. sex = row[3]
  292. income = row[4]
  293. print("fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
  294. (fname, lname, age, sex, income ))
  295. except:
  296. print("Error: unable to fecth data")
Add Comment
Please, Sign In to add comment