0

I am taking data from a CCD while moving a stage and displaying the live updating image from the CCD. The live plot works, but as soon as I hit the start button to start the stage the whole GUI freezes. I want to get the live image from the CCD while the stage is moving. Any help would be appreciated to fix this issue.

The minimal code for the loop is as follows

    movepos = np.linspace(startpos,endpos,Np1)
    dm = movepos[1]-movepos[0]
    
    tic = time.time()
    act_pos = motor.position
    
    def start():
        global diffposvec
        motor.move_to(movepos[0])
        for count in range(0,Np1):
            movetopos = movepos[count]
            motor.move_to(movetopos)
            # while motor.is_in_motion: #having this while statement makes it stop before it moves again
            #     toc = time.time()
                
            # def live_spectrum2():
            global framer
            ax2.cla()
            framer= cammer.grab_image(timeout='1s',copy=True,n_frames=1,exposure_time='5ms',cx=420,
                                              left=200,cy=600,top=300)
            ax2.imshow(framer)
            canvas2.draw()
            canvas2.get_tk_widget().place(x=5,y=5)
            # fig_plot.after(50,live_spectrum2)
                    
            # fig_plot=tk.Frame(self)
            # tk.Frame(live_spectrum2()).pack()
                            
            framer = cammer.grab_image(timeout='1s',copy=True,n_frames=1,exposure_time='5ms',
                                       cx=420,left=200,cy=600,top=300)
            heighter = np.shape(framer)[0]
            widther = np.shape(framer)[1]
            framemat = np.zeros((heighter, widther ,Np1))
            horzmat = np.zeros((Np1,widther))
            horzmat[count,:] = np.sum(framer[300:400,:],0)
            act_pos = motor.position
            diffpos = movetopos-act_pos
            diffposvec = []
            diffposvec = np.append(diffposvec,diffpos)
            if np.mod(count,10)==0:
                print('Step number %.0f' % (count+1),' of %.0f' % Np1, '  %.4f s' % (tic),'   %.3f um' % (act_pos*1000),'   %.3f um' % (movetopos*1000),'   %.3f nm' % (diffpos*1e6))
                
    start_btn=Button(self,text="START",font=(NormalFont,'16'),height=36,borderless=5,
                     foreground="#161327",background="#707087",
                     command=lambda: start())
    start_btn.place(x=350,y=650)
  • while in the loop, Tkinter can't update the screen. [Similar](https://stackoverflow.com/questions/42422139/how-to-easily-avoid-tkinter-freezing) [questions](https://stackoverflow.com/questions/54716716/how-to-stop-tkinter-from-freezing-from-while-loop) have already been ask, maybe one of them helps you? You probably need to do some kind of multi-threading. – MegaIng Apr 06 '21 at 16:52
  • You can just add `root.update()` at the start of the `while` loop – TheLizzard Apr 06 '21 at 16:58
  • Thanks, could you please provide an example of using the threading option for a live updating plot (so that the GUI won't freeze)? –  Apr 09 '21 at 19:03

0 Answers0