Mleila_1312
Mleila_1312

Reputation: 1

AttributeError: 'str' object has no attribute 'set' with a StringVar

from tkinter import*
from math import*
from tkinter.messagebox import showinfo, showwarning

def Resolution():
    A=a.get()
    B=b.get()
    C=c.get()
    if A not in "0123456789" or B not in "0123456789" or C not in "0123456789" :
        showwarning("Erreur", "Vous n'avez pas remplie correctement l'une des \
zones de saisie. Veuillez y entrer  nombre.")
    else:
        if int(A)==0:
            resultat=(0-float(C))/float(B)
            showwarning("Erreur","La solution de votre équation est :"+str(resultat)+ ". \
Cependant, votre équation n'est pas une équation du\
 second degré car le coefficient a vaut 0.")
        else:
            delta=float(B)**2-4*float(A)*float(C)
            if delta==0:
                x=(-float(B))/float(A)
                showinfo("X lorsque delta vaut 0","Votre équation: "+A+"x**2 + "+B+"x + "+C+"\nx = "+str(x)) 
            elif delta<0:
                showinfo("lorsque delta est inféreur à 0","Il n'y a pas de solution réelle car delta est inférieur à 0")
            elif delta>0:
                x1=((-float(B))-sqrt(delta))/(2*float(A))
                x2=((-float(B))+sqrt(delta))/(2*float(A))
                showinfo("X lorsque delta est supérieur à 0","Votre équation: "+A+"x**2 + "+B+"x + "+C+"\nx1 = "+str(x1)+"\nx2 = "+str(x2))
    A.set("")
    B.set("")
    C.set("")


Mafenetre=Tk()
Mafenetre.title('Additiion de 2 nombres')

Presentation = Label(Mafenetre, text="Entrez les coefficients de \
votre équation du second degré.\nRappel:\nforme d'une équation du \
second degré:\n ax**2+bx+c", bg="white").grid(row=0, column=0)

Label(Mafenetre, text="a").grid(row=1 , column= 0)
A=StringVar()
a=Entry(Mafenetre, textvariable=A, bg="bisque", fg="maroon")
a.focus_set()
a.grid(row=2, column=0)

Label(Mafenetre, text="b").grid(row=3 , column=0 )
B=StringVar()
b=Entry(Mafenetre, textvariable=B, bg="bisque", fg="maroon")
b.grid(row=4, column=0)

Label(Mafenetre, text="c").grid(row= 5, column= 0)
C=StringVar()
c=Entry(Mafenetre, textvariable=C, bg="bisque", fg="maroon")
c.grid(row=6, column=0)


Button(Mafenetre, text='Résoudre', command= Resolution).grid(row=7, column=0)

Mafenetre.mainloop()

This is a program to solve a quadratic equation. The program in itself works but I can't set the value of my coefficients to " " when the equation is solved (it makes it easier to solve another equation after). I searched on the forum, and the common answer is that you need a StringVar to apply set to it. But my variables (A, B, and C) are already StringVars and I can't find a solution.

I already tried to apply set to my Entry variables and I tried to define A, B, and C like normal str, not with StringVar.

P.S.: I'm french, so, it explains the content of the showwarnings or showinfos.

Upvotes: 0

Views: 467

Answers (2)

martineau
martineau

Reputation: 123413

The main problem, I think, is that you have both global and local variables named A, B, and C plus their types are different. The simplest fix seemed to be to rename the globals which are all StringVars and are referred to the least.

I also changed how you were checking to make sure the values were numeric and reformatted your code so it closely follows the PEP 8 - Style Guide for Python Code to make it more readable. I strongly suggest you to read and start following these coding guidelines because doing so will also make it more maintainable (as in easier to debug).

from tkinter import *
from math import *
from tkinter.messagebox import showinfo, showwarning


def resolution():
    A = var_A.get()
    B = var_B.get()
    C = var_C.get()
#    if A not in "0123456789" or B not in "0123456789" or C not in "0123456789":
    if not A.isdigit() or not B.isdigit() or not C.isdigit():
        showwarning("Erreur", "Vous n'avez pas remplie correctement l'une des "
                    "zones de saisie. Veuillez y entrer  nombre.")
    else:
        if int(A) == 0:
            resultat = (0-float(C)) / float(B)
            showwarning("Erreur", "La solution de votre équation est :" + str(resultat)
                        + ". Cependant, votre équation n'est pas une équation du "
                          "second degré car le coefficient a vaut 0.")
        else:
            delta = float(B)**2 - 4*float(A) * float(C)
            if delta == 0:
                x = (-float(B)) / float(A)
                showinfo("X lorsque delta vaut 0", "Votre équation: " + A + "x**2 + "
                         + B + "x + " + C + "\nx = " + str(x))
            elif delta < 0:
                showinfo("lorsque delta est inféreur à 0","Il n'y a pas de solution "
                         "réelle car delta est inférieur à 0")
            elif delta > 0:
                x1 = ((-float(B))-sqrt(delta)) / (2*float(A))
                x2 = ((-float(B))+sqrt(delta)) / (2*float(A))
                showinfo("X lorsque delta est supérieur à 0", "Votre équation: " + A
                         + "x**2 + " + B + "x + " + C + "\nx1 = " + str(x1) + "\nx2 = "
                         + str(x2))
    var_A.set("")
    var_B.set("")
    var_C.set("")

Mafenetre = Tk()
Mafenetre.title('Additiion de 2 nombres')

Presentation = Label(Mafenetre, text="Entrez les coefficients de "
                                     "votre équation du second degré.\nRappel:\n"
                                     "forme d'une équation du second degré:\n ax**2+bx+c",
                                bg="white")
Presentation.grid(row=0, column=0)

Label(Mafenetre, text="a").grid(row=1, column= 0)
var_A = StringVar()
a = Entry(Mafenetre, textvariable=var_A, bg="bisque", fg="maroon")
a.focus_set()
a.grid(row=2, column=0)

Label(Mafenetre, text="b").grid(row=3, column=0 )
var_B = StringVar()
b = Entry(Mafenetre, textvariable=var_B, bg="bisque", fg="maroon")
b.grid(row=4, column=0)

Label(Mafenetre, text="c").grid(row= 5, column=0)
var_C = StringVar()
c = Entry(Mafenetre, textvariable=var_C, bg="bisque", fg="maroon")
c.grid(row=6, column=0)

Button(Mafenetre, text='Résoudre', command= resolution).grid(row=7, column=0)

Mafenetre.mainloop()

Upvotes: 0

Jian Huang
Jian Huang

Reputation: 454

StringVar() in an entry is just performing like a read-only var. You can just access directly from the widget. Delete whatever text is inside the Entry, then insert the new one.

a.delete(0,END) ##END to delete all text
a.insert(0,"SomeString") ##Inserts new string

Or in your case, you just want it to be blank, then this is enough:

a.delete(0,END)

Upvotes: 1

Related Questions