tk2_label_button.py 835 B

123456789101112131415161718192021222324252627282930313233
  1. # View more python learning tutorial on my Youtube and Youku channel!!!
  2. # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
  3. # Youku video tutorial: http://i.youku.com/pythontutorial
  4. import tkinter as tk
  5. window = tk.Tk()
  6. window.title('my window')
  7. window.geometry('200x100')
  8. var = tk.StringVar()
  9. l = tk.Label(window, textvariable=var, bg='green', font=('Arial', 12), width=15,
  10. height=2)
  11. #l = tk.Label(window, text='OMG! this is TK!', bg='green', font=('Arial', 12), width=15, height=2)
  12. l.pack()
  13. on_hit = False
  14. def hit_me():
  15. global on_hit
  16. if on_hit == False:
  17. on_hit = True
  18. var.set('you hit me')
  19. else:
  20. on_hit = False
  21. var.set('')
  22. b = tk.Button(window, text='hit me', width=15,
  23. height=2, command=hit_me)
  24. b.pack()
  25. window.mainloop()