unable to close the tkinter after using destroy()

时间:2019-04-23 15:07:08

标签: python-3.x tkinter

Using tkinter window to get some user parameter and stored in dictionary, after the "SAVE" button pressed. But the tkinter is not closing in console.. How to close the tkinter after receiving the data...

import tkinter as tk
riskparams = {}

This is the simplified function with reproduce the issue here

def setriskmgmtparams():
    risk = tk.Tk()
    risk.title("Risk Management System")
    w = 650
    h = 450
    ws = risk.winfo_screenwidth()
    hs = risk.winfo_screenheight()
    x = ws - w
    y = hs - h
    risk.geometry('%dx%d+%d+%d' % (w, h, x, y))

    def savedata(*event):
        global riskparams
        riskparams['MaxQty'] = int(MQ_box.get())                
        #Setting Price % Range, if any not correct reset to defaults 7%
        riskparams['PriceLowRange'] = float(pricerange1.get())/100 
        riskparams['PriceHighRange'] = float(pricerange2.get())/100                    
        riskparams['MaxOrderVal'] = int(maxorderval.get())
        riskparams['Norderpsec'] = int(norderval.get())
        riskparams['MaxTurnover'] = int(maxturnover.get())                                
        risk.destroy()
        print(" TTAPI | RMS checks has been set ") 
        return

    rows = 0
    while rows < 11:
        risk.rowconfigure(rows, weight=1)
        risk.columnconfigure(rows, weight=1)
        rows +=1

    orderlevel = tk.Label(risk, text="RMS CHECKS PARAMS by API")
    orderlevel.grid(row=0, column=1, sticky='NESW')

    MaxQuantity = tk.Label(risk, text="Max Quantity per order:")
    MaxQuantity.grid(row=1, column=1, sticky='E')
    mqvar = tk.StringVar(risk, value='100000')
    MQ_box = tk.Entry(risk, textvariable=mqvar)
    MQ_box.bind("<Return>", savedata)
    MQ_box.grid(row=1, column=2, sticky='W')

    #price range
    price1 = tk.Label(risk, text="Price Range % LOW:")
    price1.grid(row=2, column=1, sticky='E')
    pricevar1 = tk.StringVar(risk, value='-10')
    pricerange1 = tk.Entry(risk, textvariable=pricevar1)
    pricerange1.bind('<Return>', savedata)
    pricerange1.grid(row=2, column=2, sticky='W')
    price2 = tk.Label(risk, text="Price Range % HIGH:")
    price2.grid(row=3, column=1, sticky='E')
    pricevar2 = tk.StringVar(risk, value='10')
    pricerange2 = tk.Entry(risk, textvariable=pricevar2)
    pricerange2.bind('<Return>', savedata)
    pricerange2.grid(row=3, column=2, sticky='W')

    ordval = tk.Label(risk, text="Max Order Value per order :")
    ordval.grid(row=4, column=1, sticky='E')
    mxordval = tk.StringVar(risk, value='2000000')
    maxorderval = tk.Entry(risk, textvariable=mxordval)
    maxorderval.bind('<Return>', savedata)
    maxorderval.grid(row=4, column=2, sticky='W')

    norders = tk.Label(risk, text="No Orders allowed per Second/Client :")
    norders.grid(row=5, column=1, sticky='E')
    nordval = tk.StringVar(risk, value='2')
    norderval = tk.Entry(risk, textvariable=nordval)
    norderval.bind('<Return>', savedata)
    norderval.grid(row=5, column=2, sticky='W')

    maxturn = tk.Label(risk, text="Max TurnOver per day/Client :")
    maxturn.grid(row=6, column=1, sticky='E')
    maxturnval = tk.StringVar(risk, value='10000000')
    maxturnover = tk.Entry(risk, textvariable=maxturnval)
    maxturnover.bind('<Return>', savedata)
    maxturnover.grid(row=6, column=2, sticky='W')

    savebtn = tk.Button(risk, 
                        text='SAVE', 
                        bg = "yellow",
                        command=savedata)
    savebtn.bind('<Return>', savedata)
    savebtn.grid(row=8, column=2, sticky='NESW')      

    #Run the app
    risk.mainloop()

Calling the function But the tkinter window closes and loop not closed, how to close the loop

setriskmgmtparams()

0 个答案:

没有答案