在python中是否有一种关闭系统的简洁方法

时间:2014-02-18 11:53:26

标签: python

所以我想知道的是:有没有办法能够退出一些python代码而不会出现所有这些:

Traceback (most recent call last):
  File "H:\My Documents\Year 13\COMP4\trial.3.1.4.1.py", line 590, in <module>
    myApp.monitorInput()
  File "H:\My Documents\Year 13\COMP4\trial.3.1.4.1.py", line 578, in monitorInput
    self.close()
  File "H:\My Documents\Year 13\COMP4\trial.3.1.4.1.py", line 293, in close
    sys.exit()
SystemExit

这看起来好像是一个错误,但实际上我正在做的就是退出一些代码。我不介意它出现但如果它作为一个错误信息存在,它看起来并不整洁和好。

以下是我的代码中我退出正在运行的程序的区域:

def close(self):
    print ("pygame quit")
    pygame.quit()
    sys.exit()

所有这一切的主要领域:

def monitorInput(self):

    clock = pygame.time.Clock()

    RUNNING = True
    while RUNNING:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                RUNNING = False
                break

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    self.show_keyboard = not self.show_keyboard
                    self.show_panes = not self.show_panes

            if event.type == pygame.MOUSEBUTTONUP:

                textSelected = self.mouseDown()
                print(textSelected)

                if textSelected == "Keyboard":
                    self.createKeyboard()

                elif textSelected == "Boxes":
                    self.createWords()

                elif textSelected == "Set Phrases":
                    self.createPhrases()  

                elif textSelected == "OK":
                    self.okButton()

                elif textSelected != "Nothing selected":
                    if self.show_keyboard:
                        self.terminal.addText(textSelected)
                    else:
                        self.terminal.addWord(textSelected)

                # --- draws terminal to reflect the additions ---
                if self.show_terminal:
                    self.terminal.draw()
                    pygame.display.update()
                #end if

            #end if

        #end for


        pygame.display.update()

        # --- FPS ---

        clock.tick(60)

    #end while
    self.close()

1 个答案:

答案 0 :(得分:6)

如何退出:

exit(1)