2016-09-20 122 views
0
from tkinter import * 
import math 
import sys 

def quit(): 
    root.destroy() 

def a_b_c(): 
    print_a() 
    print_b() 
    print_c() 
    calculation() 
    return 

def print_a(): 
    get_a = a.get() 
    printing_a = Label(root, text=get_a).grid(row=8, column=1) 
    return 

def print_b(): 
    get_b = b.get() 
    printing_b =Label(root, text=get_b).grid(row=12, column=1) 
    return 

def print_c(): 
    get_c = c.get() 
    printing_c =Label(root, text=get_c).grid(row=16, column=1) 
    return 


root = Tk() 
a = StringVar() 
b = StringVar() 
c = StringVar() 

root.title('Solving Quadratic Equations') 

quit_b = Button(root, text="quit",command=quit).grid(row=1, column=1) 
go_b = Button(root, text="go", command=a_b_c).grid(row=1, column=2) 
welcome = Label(root, text="Welcome to Solving Quadratic Equations with GlaDOS",font=("Helvetica",13)) 
welcome.grid(row=2, column=1) 


instructions = Label(root, text="So how do i use this program? is what you may ask yourself. So, for example, \n you have the equation 2x^2+5x+8=0. So the 2x^2 is a but you don't put the\n whole thing in you just but the 2 from the start in. The next thing is b\n in this case b = 5 and c is equal to 8. Once you have all you number in the boxes \n hit the go button. Remember you don't need the x's. ", font=("Helvetica",11)) 
instructions.grid(row=3, column=1) 

line = Label(root, text="************************************************************************************************************").grid(row=4, column=1) 

input_a = Label(root, text="Pls input A here", font=("Helvetica",11)).grid(row=6, column=1) 
entry_a = Entry(root,textvariable=a).grid(row=7,column=1) 

line = Label(root, text="************************************************************************************************************").grid(row=9, column=1) 

input_b = Label(root, text="Pls input B here", font=("Helvetica",11)).grid(row=10, column=1) 
entry_b = Entry(root,textvariable=b).grid(row=11,column=1) 

line = Label(root, text="*************************************************************************************************************").grid(row=13, column=1) 
input_c = Label(root, text="Pls input C here", font=("Helvetica",11)).grid(row=14, column=1) 
entry_c = Entry(root,textvariable=c).grid(row=15,column=1) 

two_a = a.get 
two_b = b.get 
two_c = c.get 

d = two_b**2-4*two_a*two_c 
def calculation(): 
    if d < 0: 
    no_solution = Label(root, text="This equation has no real solution").grid(row=19, column=1) 
    elif d == 0: 
    x = (-two_b+math.sqrt(two_b**2-4*two_a*two_c))/2*two_a 
    one_solution = Label(root, text="This equation has one solutions: {}".format(x)).grid(row=20, column=1) 
    else: 
    x1 = (-two_b+math.sqrt((two_b**2)-(4*(two_a*two_c))))/(2*two_a) 
    x1 = (-two_b-math.sqrt((two_b**2)-(4*(two_a*two_c))))/(2*two_a) 
    two_solution= label(root, text="This equation has two solutions: {} or {} ".format(x1,x2)).grid(row=21, colum=1) 

root.mainloop() 

爲什麼它說**或pow()方法不支持的操作數類型?有人可以幫助我改變它,所以它適用於學校,老師不能幫助我。我試圖做一個程序,可以幫助您解決二次方程,並在底部dosent工作的一部分(DEF計算) 感謝幫助我:)**或pow()方法不支持的操作數類型:'method'和'int'

+0

錯誤是指什麼? –

+0

「計算」中的代碼沒有正確縮進。 –

回答

2

您正在分配幾個值:

two_a = a.get 
two_b = b.get 
two_c = c.get 

,然後做算了一筆賬:

d = two_b**2-... 

然而,a.get是檢索StringVar的值的方法。要實際調用它並檢索該值,必須......用圓括號將其稱爲:

two_a = a.get() 

此外,您將會有字符串。它們轉換成整數或浮點數與intfloat

two_a = int(a.get()) 
# or: 
two_a = float(a.get()) 

然後按照預期的算術會工作。

0

請閱讀並研究有關mcves的SO幫助頁面。 這是一個基於你發佈的代碼的mcve。它在最後一行產生完全相同的錯誤信息。

import tkinter as tk 
root = tk.Tk() 
b = tk.StringVar(root) 
two_b = b.get 
d = two_b**2 

提高功率的方法是荒謬的。您需要調用()方法並將字符串轉換爲數字。通過使用DoubleVar或IntVar而不是StringVar或在對算法進行算術運算之前將get的結果傳遞給float()或int()來執行後者。

相關問題