Python Forum
Increase Numbers forever and need reset? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Increase Numbers forever and need reset? (/thread-30498.html)



Increase Numbers forever and need reset? - ATARI_LIVE - Oct-23-2020

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?


RE: Increase Numbers forever and need reset? - deanhystad - Oct-23-2020

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()



RE: Increase Numbers forever and need reset? - ATARI_LIVE - Oct-23-2020

(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?


RE: Increase Numbers forever and need reset? - deanhystad - Oct-23-2020

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.


RE: Increase Numbers forever and need reset? - ATARI_LIVE - Oct-23-2020

(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.