![](/img/trans.png)
[英]AttributeError: type object 'class' has no attribute 'stringVar'
[英]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()
這是一個求解二次方程的程序。 該程序本身可以工作,但是在求解方程時我無法將系數的值設置為“”(這樣可以更容易地求解另一個方程)。 我在論壇上搜索過,常見的答案是您需要一個StringVar
來將 set 應用到它。 但是我的變量( A
、 B
和C
)已經是StringVar
,我找不到解決方案。
我已經嘗試將set
應用於我的Entry
變量,並且我嘗試像普通str
一樣定義A
、 B
和C
,而不是使用StringVar
。
PS:我是法國人,所以,它解釋了showwarnings
或showinfos
的內容。
條目中的StringVar()
就像只讀StringVar()
一樣執行。 您可以直接從小部件訪問。 刪除條目內的任何文本,然后插入新文本。
a.delete(0,END) ##END to delete all text
a.insert(0,"SomeString") ##Inserts new string
或者在您的情況下,您只希望它是空白的,那么這就足夠了:
a.delete(0,END)
我認為,主要問題是您同時擁有名為A
、 B
和C
全局變量和局部變量,而且它們的類型不同。 最簡單的解決方法似乎是重命名全局變量,這些全局變量都是StringVar
並且被引用最少。
我還更改了您檢查的方式以確保值是數字並重新格式化您的代碼,使其緊密遵循PEP 8 - Python 代碼風格指南,使其更具可讀性。 我強烈建議您閱讀並開始遵循這些編碼指南,因為這樣做也會使其更易於維護(更易於調試)。
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.