运行python程序时有没有经历过时间延迟?我的应用程序假设在单击按钮后立即同时显示声音效果和图像。但有时在我点击按钮后,只有声音立即显示,图像没有显示,我需要滑动到另一个窗口(我正在使用Mac)并再次转回程序窗口,然后图像显示。显然存在时滞问题。但它时不时发生。我不确定我的代码中有什么问题。
这是我的代码(我将原始代码缩短到这里):
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')
我使用了许多IF / ELSE功能来确定在某些条件下应显示哪个图像和声音。可能这就是为什么会有时间延迟的原因?非常感谢您的帮助!
谢谢!