Python Forum
Increase Numbers forever and need reset?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Increase Numbers forever and need reset?
#1
Here my code for the mouse click modes 0 = released, 1 = clicked, 2 = press and hold.

It's WORKS.

from tkinter import *

lp_id = pressmode = 0

window_1 = Tk()
window_1.attributes('-fullscreen', True)
window_1.configure(bg='black')


def mouse_pressed_id(event=None):
	global lp_id, pressmode
	if event is None:
		pressmode = 2
	elif event.type == EventType.ButtonPress:
		lp_id = window_1.after(3000, mouse_pressed_id)
		pressmode = 1
	elif lp_id:
		window_1.after_cancel(lp_id)
		lp_id = None
		pressmode = 0
	print(pressmode, lp_id)


window_1.bind("<Button-1>", mouse_pressed_id)
window_1.bind("<ButtonRelease-1>", mouse_pressed_id)
window_1.mainloop()
Output:
started 1 after#920 0 None 1 after#969 0 None 1 after#14270 0 None 1 after#18103 0 None 1 after#22594 0 None 1 after#66445 0 None 1 after#77462 0 None 0 None 1 after#78519 0 None 1 after#78610 0 None 1 after#78697 2 after#78697 0 None 1 after#82448 0 None 1 after#84155 2 after#84155 0 None 1 after#95830 0 None 1 after#173427 0 None 1 after#226722 0 None 1 after#285857 0 None 1 after#290242 0 None 1 after#384418 0 None
You see the numbers after 'after#' are going forever, how can I reset it?
Reply
#2
Why do you care about the value of some event handle? If I was writing something like "after" I would make sure to create a unique handle for each event. In the code below I create and delete a Frame each time the mouse button is pressed/released. Each time I get a different ID for the frame. Would you want to reset the frame ID?s
from tkinter import *
 
mouse_repeat = None
window = None
 
root = Tk() 
 
def mouse_event(event):
    global window
    if event.type == EventType.ButtonPress:
        window = Frame(root)
        print('start', window)
    elif window is not None:
        print('cancel', window)
        del window
        window = None

 
root.bind("<Button-1>", mouse_event)
root.bind("<ButtonRelease-1>", mouse_event)
root.mainloop()
Reply
#3
(Oct-23-2020, 12:23 PM)deanhystad Wrote: Why do you care about the value of some event handle?

Thought it might eat up to fill the memory? and slower?
Reply
#4
You can test if it "eats up memory" or is slower. I doubt either, but if you are concerned about that you should test and verify. Then if you have a legitimate concern you can try to figure out how to reuse the after handles. Until then your time is better spent writing code.
Reply
#5
(Oct-23-2020, 01:48 PM)deanhystad Wrote: You can test if it "eats up memory" or is slower. I doubt either, but if you are concerned about that you should test and verify. Then if you have a legitimate concern you can try to figure out how to reuse the after handles. Until then your time is better spent writing code.

I see the task performance seem not effects... I guess I have to ignore it. thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,290 Jan-21-2022, 06:24 PM
Last Post: mcva
  How to increase the size of a png picture for the heatmap of the correlation? lulu43366 9 3,387 Oct-06-2021, 04:15 PM
Last Post: deanhystad
  generating random string unique forever Skaperen 5 2,286 Aug-20-2021, 07:15 AM
Last Post: Gribouillis
  Using SoX in Python to Increase mp3 Bitrate DRT 1 1,714 Jul-10-2021, 08:41 PM
Last Post: DRT
  Clicker count increase by 1 each time blakefindlay 1 5,528 Feb-03-2021, 03:50 PM
Last Post: deanhystad
  increase and decrease a slice value? KEYS 2 2,055 Nov-10-2020, 11:35 PM
Last Post: KEYS
  python multiprocessing import Pool, cpu_count: causes forever loop | help to remove Hassibayub 0 1,827 Jun-18-2020, 05:27 PM
Last Post: Hassibayub
  How do I add an element to a dic and increase an existing value Kanashi 2 1,820 Nov-21-2019, 02:56 PM
Last Post: ThomasL
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,667 May-09-2019, 12:19 PM
Last Post: Pleiades
  How to increase the data size SriRajesh 3 4,005 Nov-10-2018, 04:29 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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