Search code examples
pythontkintertkinter-entry

How if condition and clear the entry works in tkinter?


def Input(PIn , BIn , AIn) :
    if (PIn ==0) and (BIn ==0) and (AIn == 0)  :
         print ("ok")

    else :
         str(PIn)
         int(BIn)
         int(AIn)
         global n
         proc.append([PIn , BIn , AIn ])
         n+=1
         PIn.delete(0,END)
         BIn.delete(0,END)
         AIn.delete(0,END)
         print(proc , n)

I am trying to convert my python CODE to GUI but I have two problems. The first one is the if-statement is not working so if the user has entered 0 for all variables it will go to the else. the second one is that I can not clear the entry.


Solution

  • If you want to edit/ get a value from the Entry widget of tkinter you need to use

    tkinter.StringVar()

    
    PIn_text_var = StringVar() 
    PIn = Entry(root, textvariable=PIn_text_var)
    
    #To edit the widget's text : 
    
    PIn_text_var.set("new value") 
    
    #To get the widgets text :
    
    s = PIn_text_var.get()
    print(s) 
    # output : 'new value'
    
    

    If you want to clear the Entry widget's text :

    PIn_text_var.set("") 
    

    So as for your example :

    
    PIn_text_var = tkinter.StringVar() 
    BIn_text_var = tkinter.StringVar() 
    AIn_text_var = tkinter.StringVar()
    
    proc = []
    n = 0 
    
    def Input () :
        global n
    
        P = PIn_text_var.get()
        B = BIn_text_var.get()
        A = AIn_text_var.get() 
    
        if (P=="0") and (B=="0") and (A=="0") : 
            print ("ok") 
    
        else : 
            temp_var_p = P
            temp_var_b = int(B)
            temp_var_a = int(A)
    
            proc.append([temp_var_p, temp_var_b, temp_var_a ]) 
    
            PIn_text_var.set("")
            BIn_text_var.set("")
            AIn_text_var.set("")
            n+=1
            print(proc , n)