python - Testing a Tkinter Button object with an if statement -
i working on program needs gui buttons things, case when having questions buttons, have ran difficulties because while can activate functions buttons, cannot test wether being pressed if statement. know how use check buttons , radio buttons, have not found else remotely useful. need able tell how long pressed, , things being pressed stop when released. need way of assigning variable true while still holding click down on button, , false other time, normal button, not 1 toggles each time press.
it's not clear me you're having trouble with, took liberty of coding little gui times how long button pressed.
import tkinter tk import time class buttontimer: def __init__(self, root): self.master = root self.button = tk.button(self.master, text="press me") # notice haven't assigned button command - we're going bind mouse events instead of using built in command callback. self.button.bind('<buttonpress>', self.press) # call 'press' method when button pressed self.button.bind('<buttonrelease>', self.release) # call 'release' method when button released self.label = tk.label(self.master) self.starttime = time.time() self.endtime = self.starttime self.button.grid(row=1, column=1) self.label.grid(row=2, column=1) def press(self, *args): self.starttime = time.time() def release(self, *args): self.endtime = time.time() self.label.config(text="time pressed: "+str(round(self.endtime - self.starttime, 2))+" seconds") root = tk.tk() b = buttontimer(root) root.mainloop()
note: tested in python 2.7 changed import tkinter
tkinter
. work in 3.x, haven't tested version.
Comments
Post a Comment