Advertisement
here2share

# Tk_form_demo.py ZZZ

Aug 1st, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. # Tk_form_demo.py ZZZ
  2.  
  3. from Tkinter import *
  4.  
  5. def everyVar():
  6.     print 'listbox = '+str([zzz[int(z)] for z in listbox.curselection()])
  7.     for k,v in cv.var.items():
  8.         v=v.get()
  9.         if v and k != 'listbox': print k,'=',v
  10.     print
  11.  
  12. root=Tk()
  13. root.geometry('420x640+500+10')
  14. root.title("Tkinter Form Demo")
  15.  
  16. class Cv():
  17.     var={}
  18. cv=Cv()
  19. widgets="cb rb listbox pop spinbox scale entry label drop button Lframe xy".split()
  20. for z in widgets:
  21.     cv.var[z]=StringVar()
  22.  
  23. def key(event):
  24.     print "pressed", repr(event.char)
  25.  
  26. def callback(event):
  27.     Lframe.focus_set()
  28.     print "clicked at", event.x, event.y
  29.     cv.var['xy'].set((event.x, event.y))
  30.  
  31. Lframe=LabelFrame(root, text="Click Mouse Here", width=400, height=200)
  32. Lframe.bind("<Key>", key)
  33. Lframe.bind("<Button-1>", callback)
  34. Lframe2=Label(Lframe, text='0, 0', textvariable=cv.var['xy'], anchor='e')
  35.  
  36. listbox=Listbox(root, height=7, selectmode=EXTENDED)
  37. zzz="one two three four five".split()
  38. for item in zzz:
  39.     listbox.insert(END, item)
  40. pop=OptionMenu(root, cv.var['drop'], *zzz)
  41. z=1
  42. rb=['']
  43. for item in zzz:
  44.     rb.append(Radiobutton(root, text=item.title(), variable=cv.var['rb'], value=z))
  45.     z+=1
  46. cb=Checkbutton(root, text="Checked?", variable=cv.var['cb'], onvalue="on", offvalue="off")
  47. entry=Entry(root, textvariable=cv.var['entry'], bg='yellow')
  48. label=Label(root, textvariable=cv.var['entry'])
  49. spinbox=Spinbox(root, from_=1, to=12, textvariable=cv.var['spinbox'])
  50. scale= Scale(root, from_=0, to=100, variable=cv.var['scale'])
  51. button=Button(root, text="Click To Print Contents Of Array", command=everyVar)
  52.  
  53. for widget in (cb, rb[1], rb[2], rb[3], listbox, pop, spinbox, scale, entry, label, button):
  54.     widget.pack(anchor="w", padx=10)
  55. Lframe.pack(padx=10, pady=10)
  56. Lframe.pack_propagate(0)
  57. Lframe2.pack(side=TOP, anchor=E, padx=10)
  58. default='''
  59. scale
  60. 27
  61.  
  62. drop
  63. three
  64.  
  65. rb
  66. 2
  67.  
  68. cb
  69. on
  70.  
  71. spinbox
  72. 9
  73.  
  74. entry
  75. Hello Python World!
  76. '''[1:-1].split('\n\n')
  77. for z in default:
  78.         b,v=z.split('\n')
  79.         cv.var[b].set(v)
  80.  
  81. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement