Advertisement
Gingus

ButtonReleaseExample.py

Feb 19th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. import Tkinter as tk
  2. import time
  3. class Example(tk.Frame):
  4.  
  5.     def __init__(self, *args, **kwargs):
  6.         tk.Frame.__init__(self, *args, **kwargs)
  7.         self.button = tk.Button(self, text="Press me!")
  8.         self.text = tk.Text(self, width=40, height=6)
  9.         self.vsb = tk.Scrollbar(self, command=self.text.yview)
  10.         self.text.configure(yscrollcommand=self.vsb.set)
  11.         self.button.pack(side="top")
  12.         self.vsb.pack(side="right", fill="y")
  13.         self.text.pack(side="bottom", fill="x")
  14.         self.button.bind("<ButtonPress>", self.on_press)
  15.         self.button.bind("<ButtonRelease>", self.on_release)
  16.    
  17.     def on_press(self, event):
  18.         self.log("button was pressed")
  19.  
  20.     def on_release(self, event):
  21.         self.log("button was released")
  22.    
  23.     def log(self, message):
  24.         now = time.strftime("%I:%M:%S", time.localtime())
  25.    
  26.         self.text.insert("end", now + " " + message.strip() + "\n")
  27.         self.text.see("end")
  28.  
  29. if __name__ == "__main__":
  30.     root = tk.Tk() Example(root).pack(side="top", fill="both", expand=True)
  31.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement