Python Forum
Tkinter function to clear old canvas and start a fresh "operation"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter function to clear old canvas and start a fresh "operation"
#6
Thanks for all responses ! It is not fully clear to me yet. I will try to explain best to my ability my recent findings:
def handle_next_pick():
 
    print("handling next pick")
 
    global item_number
 
    global flag_next_device
 
    print("pick counter =",pick_counter)
 
    if(pick_counter < Counter_global): ## THIS IS ALWAYS TRUE WHEN THE OPERATION STARTS
 
        if(flag_next_device>0): //FLAG_NEXT_DEVICE IS SET BY TRIGGERING THE REMOTE SENSOR. 
 
            print("flag_next_device is higher than 1")
 
            if(item_number > 0):#only check if it divides by counter if its higher then 0 to avoid glitch
 
                if(item_number % Counter_global_temp==0):
 
                    item_number=item_number-(Counter_global_temp)
 
            app.update_rectangle_color(Device_global_temp[item_number])
 
            string = Device_global_temp[item_number]+"/"+"status"
 
            mqttc.publish(string,"ACTIVE",1)
 
            flag_next_device=0
 
               
 
            if(update_DB==1):
 
                update_DB_Quantity(myConnection)
 
            else:
 
                print("Dont need to update DB")
 
            app.after(100,handle_next_pick)
 
        else:
 
            app.after(1000,handle_next_pick)#IF THE SENSOR IS NOT SET, CHECK AGAIN AFTER 1 SECOND
            print("wait for next command")
 
    else: 
 
        app.after(1000,handle_end_of_picking)
So the code below is my main "task" code. I have addded some additional comments to explain what is going on. At the bottom of the function, I recursively call :
            app.after(1000,handle_next_pick)#IF THE SENSOR IS NOT SET, CHECK AGAIN AFTER 1 SECOND
            print("wait for next command")
This just checks whether the flag has been set every 1 second. And it continues this loop untill I complete the task.

The issue is that I want to be able to stop current task and start a new one even when I am in the middle of a current task.

Assume that I have currently started a task and this function "handle_next_pick" is being called every 1 second.

When I press the button on my GUI, it executed this function:
def New_operation(self):
  
    print("new_operation")
  
    restart_devices(myConnection,"reset")
  
    canvas.delete("all")
  
    app.after(1000,self.Application_Intro)
  
    print("new operation end")
So I obviously expect this function to clear all canvas, interrupt the current task and start a new one, however, thats not the case! After clearing canvas and displaying a new GUI , it continues to call handle_next_pick function every 1 second which is what I am trying to solve.


My recent findings are:

Instead of doing everyting when the button is pressed, I just tried to set the global flag:

def New_operation(self):
global reset
reset = 1 # this is defined as global variable therefore will be recognised outside
And then In my task code, I am constantly checking whether this flag is set:

            app.after(1000,handle_next_pick)#IF THE SENSOR IS NOT SET, CHECK AGAIN AFTER 1 SECOND
            print("wait for next command")
            # check if the flag is set to restart the program
            if (reset==1):
                reset = 0
                canvas.delete("all")
                app.after(1000,self.Application_Intro)
                return # IF RETURN USED HERE, PROGRAM RESETS AND WORKS AS EXPECT
Notice that return I have added after Calling Application_Intro. This return makes the program reset properly and start from the beggining. If I do not use return, It continues to call handle_next_pick every 1 second.
Could someone help me understand this behaviour?
Reply


Messages In This Thread
RE: Tkinter function to clear old canvas and start a fresh "operation" - by zazas321 - Oct-01-2020, 04:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 5,024 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  tkinter.TclError: can't invoke "canvas" command cybertooth 8 5,961 Feb-23-2023, 06:58 PM
Last Post: deanhystad
  Tkinter won't run my simple function AthertonH 6 3,832 May-03-2022, 02:33 PM
Last Post: deanhystad
  [Tkinter] Tkinter won't start uriel 4 2,405 Nov-17-2021, 05:05 PM
Last Post: uriel
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,834 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,519 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,006 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  [Tkinter] Clickable Rectangles Tkinter Canvas MrTim 4 8,848 May-11-2021, 10:01 PM
Last Post: MrTim
  [Tkinter] Draw a grid of Tkinter Canvas Rectangles MrTim 5 7,896 May-09-2021, 01:48 PM
Last Post: joe_momma
Thumbs Up tkinter canvas; different page sizes on different platforms? philipbergwerf 4 4,111 Mar-27-2021, 05:04 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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