here2share

# Tk_demos.py -- Basic Level

Mar 19th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.62 KB | None | 0 0
  1. # Tk_demos.py -- Basic Level
  2.  
  3. from Tkinter import *
  4.  
  5.  
  6. root = Tk()
  7.  
  8. theLabel = Label(root, text="A Tkinter Window in Python")
  9. theLabel.pack()
  10. Lab2 = Label(root, text="This is Label #2")
  11. Lab2.pack()
  12.  
  13. root.mainloop()
  14.  
  15.  
  16. theLabel = Label(None, text="Click Me", fg="blue")
  17. theLabel.pack()
  18. Btn2 = Button(None, text="Button #2*", fg="red")
  19. Btn2.pack()
  20.  
  21. root.mainloop()
  22.  
  23.  
  24. root = Tk()
  25.  
  26. frameA = Frame(root)
  27. frameA.pack()
  28. frameB = Frame(root)
  29. frameB.pack(side=BOTTOM)
  30.  
  31. BtnA = Label(frameA, text="Button #1", fg="blue")
  32. BtnA.pack()
  33. BtnB = Label(frameA, text="Button #2", fg="red")
  34. BtnB.pack()
  35. BtnC = Label(frameB, text="Button #3", fg="purple")
  36. BtnC.pack()
  37. BtnD = Label(frameB, text="Button #4", fg="green")
  38. BtnD.pack()
  39.  
  40. root.mainloop()
  41.  
  42.  
  43. root = Tk()
  44.  
  45. frameA = Frame(root)
  46. frameA.pack()
  47. frameB = Frame(root)
  48. frameB.pack(side=BOTTOM)
  49.  
  50. BtnA = Label(frameA, text="Button #1", fg="blue")
  51. BtnA.pack(side=LEFT)
  52. BtnB = Label(frameA, text="Button #2", fg="red")
  53. BtnB.pack(side=LEFT)
  54. BtnC = Label(frameB, text="Button #3", fg="purple")
  55. BtnC.pack(side=LEFT)
  56. BtnD = Label(frameB, text="Button #4", fg="green")
  57. BtnD.pack(side=LEFT)
  58.  
  59. root.mainloop()
  60.  
  61.  
  62. root = Tk()
  63.  
  64. BtnA = Label(None, text="Button #1", bg="blue")
  65. BtnA.pack()
  66. BtnB = Label(None, text="Button #2", bg="red")
  67. BtnB.pack(fill=X)
  68. BtnC = Label(None, text="Button #3", bg="purple")
  69. BtnC.pack(side=LEFT, fill=Y)
  70. BtnD = Label(None, text="Button #4", bg="green")
  71. BtnD.pack(expand=True, fill=BOTH)
  72.  
  73. root.mainloop()
  74.  
  75.  
  76. root = Tk()
  77.  
  78. LabelA = Label(None, text="Label #1", bg="blue")
  79.  
  80. LabelB = Label(None, text="Label #2", bg="red")
  81.  
  82. LabelC = Label(None, text="Label #3", bg="purple")
  83.  
  84. LabelD = Label(None, text="Label #4", bg="green")
  85.  
  86. LabelA.grid(row=0, column=1)
  87.  
  88. LabelB.grid(row=1, column=0)
  89.  
  90. LabelC.grid(row=1, column=3)
  91.  
  92. LabelD.grid(row=2, column=2)
  93.  
  94. root.mainloop()
  95.  
  96.  
  97. root = Tk()
  98.  
  99. theLabel = Label(None, text="Enter Username: ", fg="blue")
  100.  
  101. theLabel.grid(row=0, column=0)
  102.  
  103. theEntry = Entry(root)
  104.  
  105. theEntry.grid(row=0, column=1)
  106.  
  107. root.mainloop()
  108.  
  109.  
  110. root = Tk()
  111.  
  112. theLabel = Label(None, text="Enter Username: ", fg="blue")
  113.  
  114. theLabel.grid(row=0, column=0)
  115.  
  116. theEntry = Entry(root)
  117.  
  118. theEntry.grid(row=0, column=1)
  119.  
  120. Lab2 = Label(None, text="Password: ", fg="blue")
  121.  
  122. Lab2.grid(row=1, column=0)
  123.  
  124. Entry2 = Entry(root)
  125.  
  126. Entry2.grid(row=1, column=1)
  127.  
  128. theChkBtn = Checkbutton(root, text="Remember Password ")
  129.  
  130. theChkBtn.grid(columnspan=2)
  131.  
  132. root.mainloop()
  133.  
  134. root = Tk()
  135.  
  136. def sayHi():
  137.     print "Hello World! \n  -- It's Python\n"
  138.  
  139. theButton = Button(None, text="Click Me", fg="blue", command=sayHi)
  140. theButton.pack()
  141.  
  142. root.mainloop()
  143.  
  144.  
  145. root = Tk()
  146. root.geometry('400x400+1+1')
  147.  
  148. def sayHi(*arg):
  149.     print "Thanks, You Are Awesome!\n"
  150.  
  151. theLabel = Label(root, text="Please Click Anywhere On The Canvas", fg="blue")
  152. theLabel.pack()
  153. root.bind('<Button-1>', sayHi)
  154.  
  155. root.mainloop()
  156.  
  157.  
  158. root = Tk()
  159. root.geometry('200x200+1+1')
  160.  
  161. def leftClk(event):
  162.     print "Left Click"
  163.  
  164. def rightClk(event):
  165.     print "Right Click"
  166.  
  167. def spacebar(event):
  168.     print "Spacebar"
  169.  
  170. def leftKey(event):
  171.     print "Left Arrow"
  172.  
  173. def rightKey(event):
  174.     print "Right Arrow"
  175.  
  176. theLabel = Label(root, text="*** Python Bindings ***", fg="blue")
  177. theLabel.pack()
  178. root.bind('<Button-1>', leftClk)
  179. root.bind('<Button-3>', rightClk)
  180. root.bind('<space>', spacebar)
  181. root.bind('<Left>', leftKey)
  182. root.bind('<Right>', rightKey)
  183.  
  184. root.mainloop()
  185.  
  186.  
  187. root = Tk()
  188.  
  189. msg=''' *** Basic Calculator (BEDMAS) *** \n\nNote:\nAlso try 7+2*10
  190. then (7+2)*10\nWhereas...\n1st answer will be 27\n2nd answer will be 90\n'''
  191.  
  192. top = Label(root, text=msg, fg="blue")
  193. top.pack()
  194.  
  195. def evaluate(event):
  196.     data = e.get()
  197.     ans.configure(text='Answer: '+str(eval(data)))
  198.  
  199. e = Entry(root)
  200. e.bind('<Return>', evaluate)
  201. e.pack(fill=X)
  202. ans = Label(root, text='Press Enter To Calculate')
  203. ans.pack()
  204.  
  205. root.mainloop()
  206.  
  207.  
  208. import tkMessageBox
  209. tkMessageBox.showinfo("This Is The Window Title", "Hello Python Message Box")
  210.  
  211. for z in 'z'*3:
  212.     ans = tkMessageBox.askquestion("This Will Loop 3 Times", "Are You A Person?")
  213.     if ans == 'yes':
  214.         reply = "Hello Human!"
  215.     if ans == 'no':
  216.         reply = "Hello Non-Human Entity!"
  217.     #
  218.     tkMessageBox.showinfo("Message Box Demo", reply)
  219.  
  220. root = Tk()
  221. root.geometry('500x500+1+1')
  222.  
  223. def mbox(reply):
  224.     tkMessageBox.showinfo("Submenu Demo", reply)
  225. def A():
  226.     mbox("Hello A!")
  227. def B():
  228.     mbox("Hey B!")
  229. def C():
  230.     mbox("Hi C!")
  231. def X():
  232.     mbox("Greetings X!")
  233. def Y():
  234.     mbox("Bye Y!")
  235. def Z():
  236.     mbox("Welcome Onboard Z!")
  237. def about():
  238.     mbox("This Is Python In Tkinter!")
  239.  
  240. menubar = Menu(root)
  241.  
  242. # create a pulldown menu, and add it to the menu bar
  243. submenuABC = Menu(menubar, tearoff=0)
  244. submenuABC.add_command(label="Option A", command=A)
  245. submenuABC.add_command(label="Option B", command=B)
  246. submenuABC.add_command(label="Option C", command=C)
  247. submenuABC.add_separator()
  248. submenuABC.add_command(label="** Exit **", command=root.destroy)
  249. menubar.add_cascade(label="ABC", menu=submenuABC)
  250.  
  251. # create more pulldown menus
  252. submenuXYZ = Menu(menubar, tearoff=0)
  253. submenuXYZ.add_command(label="Option X", command=X)
  254. submenuXYZ.add_command(label="Option Y", command=Y)
  255. submenuXYZ.add_command(label="Option Z", command=Z)
  256. menubar.add_cascade(label="XYZ", menu=submenuXYZ)
  257.  
  258. helpmenu = Menu(menubar, tearoff=0)
  259. helpmenu.add_command(label="About", command=about)
  260. menubar.add_cascade(label="Help", menu=helpmenu)
  261.  
  262. # display the menu
  263. root.config(menu=menubar)
  264.  
  265. root.mainloop()
  266.  
  267.  
  268. root = Tk()
  269.  
  270. class Cv():
  271.     outp = ''
  272. cv = Cv()
  273.  
  274. calc = StringVar()
  275. calc.set('9+21')
  276.  
  277. ans = Label(root, textvariable=calc)
  278. ans.grid(columnspan=4)
  279.  
  280. def keypress(keyp):
  281.     if keyp or cv.outp:
  282.         cv.outp = cv.outp + str(keyp)
  283.         calc.set(cv.outp)
  284.  
  285. def equalbtn(*arg):
  286.     cv.outp = '1.0*{}'.format(cv.outp)
  287.     cv.outp = str(eval(cv.outp))
  288.     calc.set(cv.outp)
  289.  
  290. def reset(*arg):
  291.     cv.outp = ''
  292.     calc.set('0')
  293.  
  294. sym = 'DIVIDE / MULTIPLY * SUBSTRACT - ADD +'.split()
  295.  
  296. Btn = range(10)
  297. for z in Btn:
  298.     Btn[z] = Button(root, text='\n  {}  \n'.format(z), fg="blue", width=12, command=lambda z=z:keypress(z))
  299.     if not z: Btn[0].grid(row=4, column=1)
  300.     else: Btn[z].grid(row=((z-1)/3)+1, column=(z-1)%3)
  301. #
  302. z=1
  303. while sym:
  304.     oper,sign=sym.pop(0),sym.pop(0)
  305.     Btn.append('')
  306.     Btn[len(Btn)-1] = Button(root, text='\n {} \n'.format(oper), fg="red", width=12, command=lambda s=sign:keypress(s))
  307.     Btn[len(Btn)-1].grid(row=z, column=3)
  308.     z+=1
  309.  
  310. equal = Button(root, text='\n = \n', width=12, command=equalbtn)
  311. equal.grid(row=4, column=2)
  312. reset = Button(root, text='\n C \n', fg="red", width=12, command=reset)
  313. reset.grid(row=4, column=0)
  314.    
  315. root.mainloop()
  316.  
  317.  
  318. root = Tk()
  319.  
  320. canvas = Canvas(root, width=600, height=600)
  321. canvas.pack()
  322. x,y = 60,100
  323. canvas.create_oval((x,y,x+400,y+270), fill='green')
  324. canvas.create_polygon(60,100, 500,200, 580,400, 20,580, 480,300, fill='gray')
  325. canvas.create_rectangle(20,20,250,580)
  326.  
  327. root.mainloop()
  328.  
  329. root = Tk()
  330.  
  331. import random, time
  332. canvas = Canvas(root, width=600, height=600)
  333. canvas.pack()
  334.  
  335. colors = 'red orange yellow green blue purple gray aqua lightpink'.split()
  336.  
  337. def oRect(x,y,x2,y2,color=None):
  338.     canvas.create_rectangle((x,y,x2,y2),fill=color)
  339. #
  340. xy = []
  341. for x in range(1,6):
  342.     for y in range(1,6):
  343.         xy += [[x*100,y*100]]
  344. #
  345. print xy
  346.  
  347. xy2 = []
  348. while 1:
  349.     canvas.delete('all')
  350.     random.shuffle(xy) # Note: "xy = random.shuffle(xy)" will return "None"
  351.     xy2 = xy[:]
  352.     for color in colors:
  353.         x,y = xy2.pop()
  354.         x -= random.randint(0,30)
  355.         y -= random.randint(0,30)
  356.         x2 = x + random.randint(20,60)
  357.         y2 = y + random.randint(20,60)
  358.         oRect(x,y,x2,y2,color)
  359.         canvas.update()
  360.     t = time.time() + 0.4
  361.     while t > time.time(): 0
  362.  
  363. root.mainloop()
Add Comment
Please, Sign In to add comment