Create a new file called calc.py. Put the calc function in it and then in your main file import calc.py
kolosick.m188
Junior Poster in Training
72 posts since Jun 2009
Reputation Points: 16
Solved Threads: 16
There is no difference, as far a calling a function goes, between calling a function within or without a program. The following works for me with the calc() function contained in the same program file.
from Tkinter import *
def calc(one, two, result):
def addition():
add = one + 1
print(add)
result.insert(END, 'first='+str(one)+' second='+str(two)+' addition='
+str(add))
def subtraction():
subs = two - 1
print(subs)
result.insert(END, 'first='+str(one)+' second='+str(two)+' subtraction='
+str(subs))
result.insert(END, 'first='+str(one))
addition()
result.insert(END, 'second='+str(two))
subtraction()
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.call_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 call_calc(self):
calc(float(self.first.get()), float(self.second.get()), \
self.result)
root = Tk()
app = App(root)
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714