Teeban
Teeban

Reputation: 35

Ending the program-Python

I'm trying to make this program end straight away without asking the question " The program is still running,Do you want to kill it? " if the wrong password entered for the third time. Tried using 'quit()' but the program become not responding. Please help me.

import Tkinter



global root
global s
# this is the main/root window
root = Tkinter.Tk()
root.title("Stock Plus system")
root.geometry('800x600')
b2Var=Tkinter.StringVar()
s = 1


def win2():
    # this is the child window






    labelcementin = Tkinter.Label(root,text='Cement quantity in:')
    labelcementin.grid(row=1,column=1)
    labelhammerin = Tkinter.Label(root,text='Hammer quantity in;')
    labelhammerin.grid(row=2,column=1)
    labelspannerin = Tkinter.Label(root,text='Spanner quantity in:')
    labelspannerin.grid(row=3,column=1)
    labelbrickin = Tkinter.Label(root,text='Brick quantity in:')
    labelbrickin.grid(row=4,column=1)
    labelmirrorin = Tkinter.Label(root,text='Mirror quantity in:')
    labelmirrorin.grid(row=5,column=1)

    labelcementout = Tkinter.Label(root,text='Cement quantity out:')
    labelcementout.grid(row=1,column=3)
    labelhammerout = Tkinter.Label(root,text='Hammer quantity out:')
    labelhammerout.grid(row=2,column=3)
    labelspannerout = Tkinter.Label(root,text='Spanner quantity out:')
    labelspannerout.grid(row=3,column=3)
    labelbrickout = Tkinter.Label(root,text='Brick quantity out:')
    labelbrickout.grid(row=4,column=3)
    labelmirrorout = Tkinter.Label(root,text='Mirror quantity out:')
    labelmirrorout.grid(row=5,column=3)

    def calc_val():

        Total_StockIn=int(cementinVar.get())+int(hammerinVar.get())+int(spannerinVar.get())+int(brickinVar.get())+int(mirrorinVar.get())
        StockInLabel=Tkinter.Label(root,text='The total stock in is '+str(Total_StockIn))
        StockInLabel.grid(row=8,column=2)

        Total_Expenses= (int(cementinVar.get())*16)+(int(hammerinVar.get())*10)+(int(spannerinVar.get())*8)+(int(brickinVar.get())*2)+(int(mirrorinVar.get())*22)
        ExpensesLabel=Tkinter.Label(root,text='The total expenses is RM ' + str(Total_Expenses))
        ExpensesLabel.grid(row=9,column=2)

        Total_Income= (int(cementoutVar.get())*18)+(int(hammeroutVar.get())*12)+(int(spanneroutVar.get())*10)+(int(brickoutVar.get())*4)+(int(mirroroutVar.get())*25)
        IncomeLabel = Tkinter.Label(root,text = 'The Total income is RM ' +str(Total_Income))
        IncomeLabel.grid(row=8, column= 4)

        Remaining_Stock =Total_StockIn-(int(cementoutVar.get())+int(hammeroutVar.get())+int(spanneroutVar.get())+int(brickoutVar.get())+int(mirroroutVar.get()))
        RemainingLabel = Tkinter.Label(root,text = 'The remaining stock is ' + str(Remaining_Stock))
        RemainingLabel.grid(row=9, column = 4)

        Total_Profit = (Total_Income) - (Total_Expenses)
        ProfitLabel = Tkinter.Label(root,text = 'The total profit is RM ' + str(Total_Profit))
        ProfitLabel.grid(row=10, column = 3)

        quit()






    boxcementin = Tkinter.Entry(root,width=12,textvariable=cementinVar)
    boxcementin.grid(row=1, column=2)
    boxhammerin = Tkinter.Entry(root,width=12,textvariable=hammerinVar)
    boxhammerin.grid(row=2, column=2)
    boxspannerin = Tkinter.Entry(root,width=12,textvariable=spannerinVar)
    boxspannerin.grid(row=3, column=2)
    boxbrickin = Tkinter.Entry(root,width=12,textvariable=brickinVar)
    boxbrickin.grid(row=4, column=2)
    boxmirrorin = Tkinter.Entry(root,width=12,textvariable=mirrorinVar)
    boxmirrorin.grid(row=5, column=2)

    boxcementout = Tkinter.Entry(root,width=12,textvariable=cementoutVar)
    boxcementout.grid(row=1, column=4)
    boxhammerout = Tkinter.Entry(root,width=12,textvariable=hammeroutVar)
    boxhammerout.grid(row=2, column=4)
    boxspannerout = Tkinter.Entry(root,width=12,textvariable=spanneroutVar)
    boxspannerout.grid(row=3, column=4)
    boxbrickout = Tkinter.Entry(root,width=12,textvariable=brickoutVar)
    boxbrickout.grid(row=4, column=4)
    boxmirrorout = Tkinter.Entry(root,width=12,textvariable=mirroroutVar)
    boxmirrorout.grid(row=5, column=4)





    button = Tkinter.Button(root,text='Calculate',command=calc_val)
    button.grid(row=7,column=3)







def textboxvalue():
#For password entry
    global s

    if (s!=3 ):
       Password=b2Var.get()
       Username=b1Var.get()
       if Password ==('stock123'):
           label4=Tkinter.Label(root,text='Welcome to stock plus system, press login again to start using')
           label4.grid(row=0,column=3)
           Button_1 = Tkinter.Button(root, text="Login", command=win2)
           Button_1.grid(row=7,column=3)



       else:
           s =s+1
           label3=Tkinter.Label(root,text='Try again')
           label3.grid(row=3,column=1)

    else:
        label5=Tkinter.Label(root,text='bye')
        label5.grid(row=4,column=4)
        label6=Tkinter.Label(root,text='Thank You for using Stock Plus System ')
        label6.grid(row=5,column=4)







#Widgets in main window
Button_1 = Tkinter.Button(root, text="Login", command=textboxvalue)
Button_1.grid(row=7, column=3)
b1Var = Tkinter.StringVar()
b2Var = Tkinter.StringVar()

box1Label = Tkinter.Label(root,text='Username:')
box1Label.grid(row=1,column=3)
box2Label = Tkinter.Label(root,text='Password:')
box2Label.grid(row=2,column=3)
box1Text = Tkinter.Entry(root,textvariable=b1Var,width=12)
box1Text.grid(row=1, column=4)
box2Text = Tkinter.Entry(root,textvariable=b2Var,width=12,show='*')
box2Text.grid(row=2, column=4)

cementinVar = Tkinter.IntVar()
hammerinVar = Tkinter.IntVar()
spannerinVar = Tkinter.IntVar()
brickinVar = Tkinter.IntVar()
mirrorinVar = Tkinter.IntVar()

cementoutVar = Tkinter.IntVar()
hammeroutVar = Tkinter.IntVar()
spanneroutVar = Tkinter.IntVar()
brickoutVar = Tkinter.IntVar()
mirroroutVar = Tkinter.IntVar()




root.mainloop()

Upvotes: 0

Views: 203

Answers (2)

abarnert
abarnert

Reputation: 365717

You have two, or maybe three, problems with your use of quit.

First, you put it in the wrong place. You want to quit if the user fails to login three times. But your quit() call comes at the end of calcval, which only gets called after the user has successfully logged in and calculated a value, so of course it's not going to help.

Second, you called the wrong thing. The quit function is a special thing used for quitting the interactive interpreter. You usually shouldn't use it in applications at all (use sys.exit() for command-line programs), and especially not in GUI apps. The quit method is called on the same Tkinter window you called mainloop() on, and tells it to quit the main loop.

So, what you want to do is add this line inside the else block that has the Thank You for using Stock Plus System text:

root.quit()

That may not work, depending on your platform, and how you're running your program—or, if you're running from IDLE, it may cause IDLE to quit as well. Tkinter is finicky. If you have the first problem, add this line before the quit; if you have the second problem, use it instead of the quit.

root.destroy()

If you want to understand, see this thread. Briefly: destroy destroys any widget and all of its children. And when you call it on a root widget that owns the mainloop, it also ends the loop. quit sends a message to the mainloop that tells it to destroy all widgets it knows about and then kill the Tcl interpreter. Normally, it should always be safe to just quit, but there are some cases where it can miss a widget and freeze up—and when you're running under IDLE, you may be sharing the same Tcl interpreter as IDLE itself, so quit may kill or hang it as well.

Upvotes: 1

Dave Plug
Dave Plug

Reputation: 1079

Have you already tried to use

import sys
sys.exit()

or directly

raise SystemExit(0)

Upvotes: 0

Related Questions