如何停止所有进程并退出

时间:2013-04-23 09:59:18

标签: python tkinter

情况

我编写了一个完整的应用程序,充当密码管理器。我有一个存储到文件的主密码。每次用户想要执行主要操作(如更改帐户,创建新帐户,更改主密码等)时,我都会提示用户输入主密码,并验证用户输入的内容以及存储在文件中的内容。我已经定义了一个函数:

def checkpoint(self):
        ##first take the masterKey from the file

        inFile = open(FILE_NAME_Mast,'r')

        M = inFile.read()

        inFile.close()

        temp = tkSimpleDialog.askstring('master key','Enter your master key:',show='*')

        temp = temp + salt

        temp = sha1(temp)

        temp = temp.hexdigest()

        if not temp == M:

            self.quitwin()

        else:

            return

self.quitwin()是在退出应用程序之前在屏幕上显示愉快的功能。

问题

用于输入新帐户,修改现有帐户和其他流程,我所做的是创建Toplevel实例。在执行此操作时,我使用Tk撤消主root.withdraw实例。所发生的事情是,在退出之前我在屏幕上显示的任何内容都被隐藏,用户可以输入新帐户并将其存储到文件中,即使他输入了错误的主密码。这是我绝对不想要的。

目标

调用函数self.checkpoint后,消息将显示一段时间,然后应用程序应退出关闭所有窗口。基本上,我不希望控件返回到调用self.checkpoint的函数,因为这会破坏主密钥的用途。

earilier

早些时候我曾经用C ++编程。退出应用程序非常简单。我使用了以下 C ++ 代码:

cout<<"\n\nEnter the master key:";

char str[50];
char masterKey[20];

//masterkey is read from file and stored into this variable.

for(int i =0;str[i]!=13;i++) // 13 is the ASCII code of the return key
{

  str[i] = getch();
  cout<<"*";   //so that an asterisk is displayed for each character.

}

if(strcmp(str,masterKey) != 0)
{ 
  cout<<"\nWrong password";
  cout<<"\nGoodbye.";
  delay(2000);   // included dos.h
  exit(0);  //included stdlib.h
}

但是这在Python和Tkinter中证明是相当困难的。我不想通过重新构建现有的功能来改变整个程序。因此,请建议一种方法,其中包括仅需要在函数self.checkpoint中进行的更改。

编辑1

这就是我想做的事情:

  1. 询问用户密码
  2. 使用存储在文件中的内容来处理用户输入的内容:

    i)如果匹配,则返回调用函数的位置并执行进一步的语句。

    ii)如果它们不匹配,则在窗口上显示一些内容然后退出。控制应该转回到调用函数的位置。

  3. 编辑2

    这就是我得到的:

        if not temp == M:
    
            self.window.destroy()
    
            import sys
            sys.exit()
    

    但这有一个问题。这会删除所有内容,这很好。事实上,这正是我想要的。但在退出所有内容之前,我想要打印标签unauthorized access,然后退出。所以:

    1. 打印标签
    2. 等待3秒
    3. 破坏窗口
    4. 使用sys.exit()
    5. 退出应用程序

      本代码可以同时执行步骤3和步骤4,但步骤1和2是重要的,需要在其他两个之前执行。

      我已尝试使用self.window.after(3000,sys.exit),但这不起作用。在三秒之后,它会退出,但控件返回到调用checkpoint的位置,并执行那些语句。

      帮助我解决此问题并为此提供解决方案。

0 个答案:

没有答案
相关问题