如何使这个简单的Python程序解决来自GUI的给定输入的等式

时间:2015-07-21 17:56:13

标签: python tkinter

所以我有这个代码,我试图让python解决一个方程式,其中变量从GUI分配值,然后显示答案。您应该只需要提供两个变量值,其余的应该解决。我仍然是python的新手。它是一个游戏,继承人我所得到的:

from Tkinter import *

class Application(Frame):
    def __init__(self, master):
        Frame.__init__(self,master)
        self.grid()

        self.d = 0
        self.size = 0
        self.mil = 0
        self.create_widgets()

    def create_widgets(self):
        self.L1 = Label(self)
        self.L2 = Label(self)
        self.L3 = Label(self)

        self.L1.grid(column = 1, row = 1)
        self.L2.grid(column = 1, row = 2)
        self.L3.grid(column = 1, row = 3)

        self.L1["text"] = "Distance from Target " + str(self.d) + "m" 
        self.L2["text"] = "Size of Target " + str(self.size) + "mils"
        self.L3["text"] = "Mil Dot adj " + str(self.mil)


        self.entryd = Entry(self, bd=5)
        self.entryd.grid(column = 2, row = 1, padx = 10)
        self.entrysize = Entry(self, bd=5)
        self.entrysize.grid(column = 2, row = 2)
        self.entrymil = Entry(self, bd=5)
        self.entrymil.grid(column = 2, row = 3)

        self.button1 = Button(self)
        self.button1["text"] = "Calculate Kill Shot"
        self.button1["command"] = self.calc
        self.button1.grid(column = 2, row = 4)



    def calc(self):
        self.d = self.entryd.get()
        self.size = self.entrysize.get()
        self.mil = self.entrymil.get()

        self.L1["text"] = "Distance from Target " + str(self.d) + "m" 
        self.L2["text"] = "Size of Target " + str(self.size) + "mils"
        self.L3["text"] = "Mil Dot adj " + str(self.mil)

        #self.d = self.size * 1000 / self.mil
    #above is the equation i tried using. 


root = Tk()
root.title("heeey")
root.geometry("400x400")
app = Application(root)

root.mainloop()

我看到有些人使用了sympy,我试过但却无法得到它。任何信息都非常有用,谢谢!

1 个答案:

答案 0 :(得分:1)

看起来唯一的问题是由错误准确描述:

TypeError: unsupported operand type(s) for /: 'str' and 'str'

告诉你,你正在尝试用字符串分割字符串。您需要做的就是在进行计算之前将字符串转换为数字:

self.d = int(self.size) * 1000 / int(self.mil)