Python Forum
[Tkinter] Mouse click without use bind
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Mouse click without use bind
#9
(Oct-21-2020, 03:08 PM)joe_momma Wrote: here's my attempt:
from tkinter import *

import logging, sys
from time import strftime as s_time, localtime as l_time, time
logging.basicConfig(level= logging.DEBUG)
#logging.disable(logging.CRITICAL)

class Long_Key_Press(Frame):
    def __init__(self, parent=None, HOLD_TIME=10):
        self.parent= parent
        self.HOLD_TIME= HOLD_TIME
        Frame.__init__(self, self.parent)
        self.pack(expand=YES, fill=BOTH)
        
        self.canvas= Canvas(self)
        self.canvas.config(width= 500, height= 700, bg='skyblue')
        self.canvas.pack(expand=YES, fill=BOTH)
        
        self.btn= Button(self.canvas, text='QUIT', command= self.get_entry)
        self.btn.place(x=400,y=600)
        
        self.time_txt= StringVar()
        self.lbl_1= Label(self.canvas, textvariable= self.time_txt,
                          font=('arial',25,'bold'))
        self.lbl_1.place(x=20,y=50)

        self.output_lbl= StringVar()
        self.lbl_2= Label(self.canvas, textvariable= self.output_lbl,
                          font=('arial',25,'bold'))
        self.lbl_2.place(x=20,y=200)
        self.output_lbl.set('Hold left mouse down')
        
        self.bind_all('<Key>', self.key)
        self.canvas.bind('<Button-1>', self.left_mouse_down)
        self.canvas.bind('<ButtonRelease-1>', self.left_mouse_up)

        self.time_elapsed= 0.0
        
        self.counter= 0
        self.g_count=0
    def set_timer(self,elapsed):
        self.minutes= int(elapsed/60)
        self.seconds= int(elapsed- self.minutes*60.0)
        self.hseconds= int((elapsed- self.minutes*60.0 - self.seconds)*100)
        self.time_txt.set('%02d:%02d:%02d'%(self.minutes,
                                            self.seconds,
                                            self.hseconds))
    def update_time(self):
        self.time_elapsed= time() - self.start
        self.set_timer(self.time_elapsed)
        if self.seconds == self.HOLD_TIME * .25:
            self.output_lbl.set('Quarter way there...')
        elif self.seconds == self.HOLD_TIME * .5:
            self.output_lbl.set('Half way there...')
        elif self.seconds == self.HOLD_TIME * .75:
            self.output_lbl.set("3/4 the to finish...")
        elif self.seconds >= self.HOLD_TIME:
            self.output_lbl.set('One second longer....')
        
        self._timer= self.after(50, self.update_time)
    def left_mouse_down(self, event):
        x,y= event.x, event.y
        self.start= time() - self.time_elapsed
        self.update_time()
        
    def left_mouse_up(self, event):
        self.after_cancel(self._timer)
        self.time_elapsed= 0.0
        
        if self.seconds > self.HOLD_TIME:
            self.output_lbl.set("That is long enough.")
            self.canvas.config(bg='lime')
        else:
            self.output_lbl.set("Longer hold is required.")
            self.canvas.config(bg='red')
        
        
    def key(self, event):
        self.g_count +=1
        message= 'count:{0} key:{1} num:{2} state:{3}'.format(self.g_count,
                                                 event.keysym,event.keysym_num,
                                       event.state)
        logging.debug(message)
        
    def get_entry(self):
        self.parent.destroy()
if __name__ == '__main__':
    root= Tk()
    Long_Key_Press(root, HOLD_TIME=20)
    root.mainloop()

That's works but almost!... not for me. I really thankful for this.
Reply


Messages In This Thread
Mouse click without use bind - by ATARI_LIVE - Oct-01-2020, 11:59 AM
RE: Mouse click without use bind - by Larz60+ - Oct-01-2020, 05:35 PM
RE: Mouse click without use bind - by deanhystad - Oct-01-2020, 07:40 PM
RE: Mouse click without use bind - by Larz60+ - Oct-02-2020, 01:26 AM
RE: Mouse click without use bind - by ATARI_LIVE - Oct-02-2020, 09:18 AM
RE: Mouse click without use bind - by ATARI_LIVE - Oct-06-2020, 07:40 AM
RE: Mouse click without use bind - by deanhystad - Oct-06-2020, 05:46 PM
RE: Mouse click without use bind - by joe_momma - Oct-21-2020, 03:08 PM
RE: Mouse click without use bind - by ATARI_LIVE - Oct-23-2020, 10:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - touchscreen, push the button like click the mouse John64 5 870 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  [Tkinter] bind menator01 1 1,265 Apr-15-2022, 08:47 PM
Last Post: menator01
  [Tkinter] bind lambda keypress counter knoxvilles_joker 15 7,815 Apr-19-2021, 01:56 AM
Last Post: knoxvilles_joker
  [Tkinter] program unresponsive during pynput mouse click RobotTech 1 3,504 May-07-2020, 04:43 PM
Last Post: RobotTech
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 6,282 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  [WxPython] Bind error PeterLinux 1 2,258 Apr-06-2020, 03:07 AM
Last Post: joe_momma
  Tkinter:Unable to bind and unbind function with a button shallanq 2 5,051 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 3,775 Dec-16-2019, 04:47 AM
Last Post: woooee
  [Tkinter] How to bind an event when enter is pressed on a Entry control? Michael4 4 3,972 Aug-29-2019, 10:11 PM
Last Post: Michael4
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,034 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020