Thursday, 19 September 2013

new to tkinter and python trying simple math using GUI

new to tkinter and python trying simple math using GUI

Trying to make a very basic addition calculator with python and tkinter.
It gives me an error: btresult = Button(window, text = "Compute Sum",
command = self.result).grid(row = 4, column = 2, sticky = E) ^
SyntaxError: invalid syntax
I am having trouble figuring out how to connect this.
from tkinter import *
class addCalculator: def init(self): window = Tk() window.title("Add
Calculator")
Label(window, text = "First Number: ").grid(row = 1, column = 1,
sticky = W)
Label(window, text = "Second Number: ").grid(row = 2, column = 1,
sticky = W)
self.number1Var = StringVar()
Entry(window, textvariable = self.number1Var, justify =
RIGHT).grid(row = 1, column = 2)
self.number2Var = StringVar()
Entry(window, textvariable = self.number2Var, justify =
RIGHT).grid(row = 2, column = 2)
self.resultVar = StringVar()
lblresult = Label(window, textvariable = self.result.grid(row = 3,
column = 2, sticky = E)
btresult = Button(window, text = "Compute Sum", command =
self.result).grid(row = 4, column = 2, sticky = E)
def result(self):
resultVar = self.resultVar.set(eval(self.number1Var.get()) +
eval(self.number2Var.get()))
return resultVar
window.mainloop()
addCalculator()

No comments:

Post a Comment