循环pyqt窗口

时间:2015-07-28 11:53:44

标签: python loops while-loop pyqt

我希望使用' while'循环打开关闭后打开相同的PyQT窗口,但由于某种原因,窗口冻结并在尝试循环时停止响应。我已经尝试了所有我知道的,但似乎没有什么能解决它。有什么建议吗?

    while active=="y":
        formclass, baseclass = uic.loadUiType("mainwindow.ui")
        class MyForm(baseclass, formclass):
           def __init__(self, parent=None):
              QtGui.QMainWindow.__init__(self, parent)
              self.setupUi(self)
              self.steps_to_battle = random.randint(1,25)
              self.runclicks=0
              self.bikeclicks=0
              self.steps = 0
              self.actionFeed.triggered.connect(self.feed_Click)
              self.actionWalk.triggered.connect(self.walk_Click)
              self.actionPlay.triggered.connect(self.play_Click)
              self.actionDoctor.triggered.connect(self.doctor_Click)
              self.actionRun.triggered.connect(self.run_Click)
              self.actionBike.triggered.connect(self.bike_Click)
              self.doctor = False
              self.walking = False
              self.playing = False
              self.eating = False
              self.sleeping = False
              self.time_cycle = 0
              self.hunger = 0
              self.happiness  = 8
              self.health = 8
              self.forceAwake = False
              self.doctorImages =["walk_forward1.png","walk_forward2.png","walk_forward3.png","walk_forward2.png"]
              self.eatImages = ["walk_backward1.png", "walk_backward2.png", "walk_backward3.png", "walk_backward2.png"]
              self.walkImages = ["walk_left1.png", "walk_left2.png", "walk_left3.png", "walk_left2.png"]
              self.playImages = ["walk_right1.png", "walk_right2.png", "walk_right3.png", "walk_right2.png"]
              self.nothingImages  = ["stand_forward.png", "stand_left.png", "stand_backward.png", "stand_right.png"]
              self.imageList = self.nothingImages
              self.imageIndex = 0
              self.myTimer1 = QtCore.QTimer(self)
              self.myTimer1.start(500)
              self.myTimer1.timeout.connect(self.animation_timer)
              self.myTimer2 = QtCore.QTimer(self)
              self.myTimer2.start(5000)
              filehandle = True
              try:
                  file = open("savedata_vp.pkl", "r")
              except:
                  filehandle = False
              if filehandle:
                  save_list = pickle.load(file)
                  file.close()
              else:
                  save_list = [8, 8, 0, datetime.datetime.now(), 0]
              self.happiness = save_list[0]
              self.health    = save_list[1]
              self.hunger    = save_list[2]
              timestamp_then = save_list[3]
              self.time_cycle = save_list[4]
              difference = datetime.datetime.now() - timestamp_then
              ticks = difference.seconds / 50
              for i in range(0, ticks):
                  self.time_cycle += 1
              self.imageList = self.nothingImages
          def run_Click(self):
              self.runclicks=self.runclicks+1
              if self.runclicks==1:
                  self.bikeclicks=0
                  self.doctorImages = ["run_forward1.png","run_forward2.png","run_forward3.png","run_forward2.png"]
                  self.eatImages = ["run_backward1.png", "run_backward2.png", "run_backward3.png", "run_backward2.png"]
                  self.walkImages = ["run_left1.png", "run_left2.png", "run_left3.png", "run_left2.png"]
                  self.playImages = ["run_right1.png", "run_right2.png", "run_right3.png", "run_right2.png"]
              else:
                  self.runclicks=0
                  self.doctorImages = ["walk_forward1.png","walk_forward2.png","walk_forward3.png","walk_forward2.png"]
                  self.eatImages = ["walk_backward1.png", "walk_backward2.png", "walk_backward3.png", "walk_backward2.png"]
                  self.walkImages = ["walk_left1.png", "walk_left2.png", "walk_left3.png", "walk_left2.png"]
                  self.playImages = ["walk_right1.png", "walk_right2.png", "walk_right3.png", "walk_right2.png"]
          def bike_Click(self):
              self.bikeclicks=self.bikeclicks+1
              if self.bikeclicks==1:
                  self.runclicks=0
                  self.doctorImages = ["bike_forward1.png","bike_forward2.png","bike_forward3.png","bike_forward2.png"]
                  self.eatImages = ["bike_backward1.png", "bike_backward2.png", "bike_backward3.png", "bike_backward2.png"]
                  self.walkImages = ["bike_left1.png", "bike_left2.png", "bike_left3.png", "bike_left2.png"]
                  self.playImages = ["bike_right1.png", "bike_right2.png", "bike_right3.png", "bike_right2.png"]
                  self.nothingImages  = ["bike_stop_forward.png", "bike_stop_left.png", "bike_stop_backward.png", "bike_stop_right.png"]
                  self.imageList = self.nothingImages
                  self.imageIndex = 0
              else:
                  self.bikeclicks=0
                  self.doctorImages = ["walk_forward1.png","walk_forward2.png","walk_forward3.png","walk_forward2.png"]
                  self.eatImages = ["walk_backward1.png", "walk_backward2.png", "walk_backward3.png", "walk_backward2.png"]
                  self.walkImages = ["walk_left1.png", "walk_left2.png", "walk_left3.png", "walk_left2.png"]
                  self.playImages = ["walk_right1.png", "walk_right2.png", "walk_right3.png", "walk_right2.png"]
                  self.nothingImages  = ["stand_forward.png", "stand_left.png", "stand_backward.png", "stand_right.png"]
          def doctor_Click(self):
              self.imageList = self.doctorImages
              self.doctor = True
              self.walking = False
              self.eating = False
              self.playing = False
          def feed_Click(self):
              self.imageList = self.eatImages
              self.eating = True
              self.walking = False
              self.playing = False
              self.doctor = False
          def play_Click(self):
              self.imageList = self.playImages
              self.playing = True
              self.walking = False
              self.eating = False
              self.doctor = False
          def walk_Click(self):
              self.imageList = self.walkImages
              self.walking = True
              self.eating = False
              self.playing = False
              self.doctor = False
          def stop_Click(self):
              self.imageList = self.nothingImages
              self.walking = False
              self.eating = False
              self.playing = False
          def animation_timer(self):
              self.imageIndex += 1
              if self.imageIndex >= len(self.imageList):
                  self.imageIndex = 0
              icon = QtGui.QIcon()
              current_image = self.imageList[self.imageIndex]
              if self.doctor==True or self.eating==True or self.playing==True or self.walking==True:
                  if self.runclicks==0 and self.bikeclicks==0:
                       self.steps=self.steps+0.5
                  elif self.runclicks==1 and self.bikeclicks==0:
                       self.steps=self.steps+1
                  elif self.runclicks==0 and self.bikeclicks==1:
                       self.steps=self.steps+1.5
              if self.steps>=self.steps_to_battle:
                  easygui.msgbox("A wild Pokemon appeared!")
                  self.close()
              icon.addPixmap(QtGui.QPixmap(current_image),
                             QtGui.QIcon.Disabled, QtGui.QIcon.Off)
              self.petPic.setIcon(icon)
          def closeEvent(self, event):
              file = open("savedata_vp.pkl", "w")
              save_list = [self.happiness, self.health, self.hunger, datetime.datetime.now(), self.time_cycle]
              pickle.dump(save_list, file)
              event.accept()
          def menuExit_selected(self):
              self.close()
      app = QtGui.QApplication(sys.argv)
      myapp = MyForm()
      myapp.show()
      app.exec_()

0 个答案:

没有答案