0

Has any of you experienced time delay when running your python program? My app suppose to present a sound effect and a image simultaneously right after I click button. But sometimes after I clicked the button, only the sound shows immediately, and the image doesn't show up, and I need to swipe to another window (I'm using Mac)and turn back the program window again, the image then shows. So clearly there's time lag issue. But it happens time to time. I'm not sure what's wrong in my code.

Here is my code (I shorten the original code to put it here):

    self.choiceA = self.master.bind('a', self.run1)
    self.choiceB = self.master.bind('l', self.run2)


def run1(self, event=None):
    self.draw_confirmation_button1()
    t = Timer(1, self.run1_s2)
    t.start()

def run1_s2(self):     
    self.clickPreProcess(False)

def run2(self, event=None):
    self.draw_confirmation_button2()
    t = Timer(1, self.run2_s2)
    t.start()

def run2_s2(self):
    self.clickPreProcess(True)


def clickPreProcess(self, human_choice):
    self.showResult()

def showResult(self):
    #show sounds#
    if self.value < 0:
        pygame.mixer.init()
        pygame.mixer.music.load("/Users/kezhang/Desktop/cues/sounds/loss/Trimed/2_matched.wav")
        pygame.mixer.music.play()

    elif self.value > 0:
        pygame.mixer.init()
        pygame.mixer.music.load("/Users/kezhang/Desktop/cues/sounds/wins/coin2_matched.wav")
        pygame.mixer.music.play()

    else:
        None

   #show images#
    if 1<=self.value<6:
        imgFile = Image.open('/Users/kezhang/Desktop/cues/images/c1_rs.png')
        self.imDisplay = ImageTk.PhotoImage(imgFile)
        self.image = self.w.create_image(653, 384, image=self.imDisplay, anchor='center')
        #self.coin1 = Label(self.w, image=imgFile)

    if 6<=self.value<12:
        imgFile = Image.open('/Users/kezhang/Desktop/cues/images/c2_rs.png')
        self.imDisplay = ImageTk.PhotoImage(imgFile)
        self.image = self.w.create_image(653, 384, image=self.imDisplay, anchor='center')
        #self.coin2 = Label(self.w, image=imgFile)

    if 12<=self.value<18:
        imgFile = Image.open('/Users/kezhang/Desktop/cues/images/c3_rs.png')
        self.imDisplay = ImageTk.PhotoImage(imgFile)
        self.image = self.w.create_image(653, 384, image=self.imDisplay, anchor='center')
        #self.coin3 = Label(self.w, image=imgFile)

    if 18<=self.value<24:
        imgFile = Image.open('/Users/kezhang/Desktop/cues/images/c4_rs.png')
        self.imDisplay = ImageTk.PhotoImage(imgFile)
        self.image = self.w.create_image(653, 384, image=self.imDisplay, anchor='center')
        #self.coin4 = Label(self.w, image=imgFile)

    if 24<=self.value<=30:
        imgFile = Image.open('/Users/kezhang/Desktop/cues/images/c5_rs.png')
        self.imDisplay = ImageTk.PhotoImage(imgFile)
        self.image = self.w.create_image(653, 384, image=self.imDisplay, anchor='center')
        #self.coin5 = Label(self.w, image=imgFile)


    if -1>=self.value>-6:
        imgFile = Image.open('/Users/kezhang/Desktop/cues/images/c1_loss_rs.png')
        self.imDisplay = ImageTk.PhotoImage(imgFile)
        self.image = self.w.create_image(653, 384, image=self.imDisplay, anchor='center')
    if -6>=self.value>-12:
        imgFile = Image.open('/Users/kezhang/Desktop/cues/images/c2_loss_rs.png')
        self.imDisplay = ImageTk.PhotoImage(imgFile)
        self.image = self.w.create_image(653, 384, image=self.imDisplay, anchor='center')
    if -12>=self.value>-18:
        imgFile = Image.open('/Users/kezhang/Desktop/cues/images/c3_loss_rs.png')
        self.imDisplay = ImageTk.PhotoImage(imgFile)
        self.image = self.w.create_image(653, 384, image=self.imDisplay, anchor='center')
    if -18>=self.value>-24:
        imgFile = Image.open('/Users/kezhang/Desktop/cues/images/c4_loss_rs.png')
        self.imDisplay = ImageTk.PhotoImage(imgFile)
        self.image = self.w.create_image(653, 384, image=self.imDisplay, anchor='center')
    if -24>=self.value>=-30:
        imgFile = Image.open('/Users/kezhang/Desktop/cues/images/c5_loss_rs.png')
        self.imDisplay = ImageTk.PhotoImage(imgFile)
        self.image = self.w.create_image(653, 384, image=self.imDisplay, anchor='center')

I have used many IF/ELSE function to determine which image and sound should be shown on some conditions. Possibly that's the reason why there's time delay? Really appreciate for your help!

Thanks!

key
  • 63
  • 3
  • 9
  • 1
    Currently, all of the `if` statements are being evaluated. Even if a condition is found to be `True`, e.g. `if -1>=self.value>-6:` you will continue to test the other conditions that cannot be `True`. This is inefficient, use `if/elif`. Whether that's enough to cause this delay, I'm not sure. – roganjosh Feb 09 '17 at 18:06
  • 1
    Also, you should use a [DRY](https://en.wikipedia.org/wiki/Don't_repeat_yourself) approach here and get rid of all the repetition. All those `if` checks basically do the same thing but just load a slightly different file. It can all be condensed. Also consider whether you can use a dictionary to map these things. – roganjosh Feb 09 '17 at 18:10
  • 1
    Preload your images and sound files. You dont really want to be waiting to read from disk on a button click. – cmd Feb 09 '17 at 19:26
  • Thanks for your help! I pre-load the images and sounds at "init", and retrieve the corresponding image/sound at the `def showResult(self)`. Another follow-up question. Because I need to clear images/sound after 2s, and update new question which users need to respond, so I wrote `def clearResult(self): self.w.itemconfigure(self.image, state='hidden') self.displayQuestion()`. But occasionally the program fails to show new question and clear image (stuck on screen with time delay). I'm unsure why it delays and the pattern it delays (e.g., looks random?). Could you help me? Really appreciate it! – key Feb 10 '17 at 09:39

0 Answers0