Hi,
I am new in Daniweb. I am trying to do a simple task in Python but cannot figure out how it works. Keeps getting it wrong.
Basically, I have a main GUI class App which has 2 entry boxes where you fill two numbers, a listbox where the results are produced.
a button calls a function def calc(self):
which gets these two numbers and performs simple calculations like addition multiplication etc and prints them in the listbox.
I want this simple function calc(self)
to be in a separate file and still be able to print the result in the GUI listbox.
Can anyone please help me with this.
here is the sample code:
from Tkinter import *
class App:
def __init__(self, parent):
frame = Frame(parent.title("primary window"))
frame.pack()
self.lbfirst = Label(frame, text="First number:")
self.lbfirst.grid(row=0,column=0)
self.first = Entry(frame)
self.first.grid(row=1,column=0)
self.lbsecond = Label(frame, text="Second number:")
self.lbsecond.grid(row=2,column=0)
self.second = Entry(frame)
self.second.grid(row=3,column=0)
self.number=Button(frame, text="calc", fg="red", command=self.calc)
self.number.grid(row=4,column=0)
self.result = Listbox(frame)
self.result.grid(row=5,column=0, columnspan=5, sticky=N+S+E+W)
def calc(self):
one = float(self.first.get())
two = float(self.second.get())
def addition():
add = one + 1
print(add)
self.result.insert(END, 'first='+str(one)+' second='+str(two)+' addition='
+str(add))
def subtraction():
subs = two - 1
print(subs)
self.result.insert(END, 'first='+str(one)+' second='+str(two)+' subtraction='
+str(subs))
self.result.insert(END, 'first='+str(one))
addition()
self.result.insert(END, 'second='+str(two))
subtraction()
root = Tk()
app = App(root)
root.mainloop()