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()

Create a new file called calc.py. Put the calc function in it and then in your main file import calc.py

Thx for your reply kolosick.m188.

I have tried that. It does the calculation but it fails to print the result in the listbox back in the main GUI.

when i pass the variables "one" and "two" from main file to the calc.py function, it receives it ok and does add, subtract etc (i have used the print command to check)
But it fails when I want to go back in the main file and put the result in the listbox.

do you have any idea about this?

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)

Thanks wooee!! a few changes here and there and it does the job! do you know why if i put the function in a separate file, it does not recognise END term in the
result.insert(END, 'first=', str(one)...)

it prompts the error : NameError: global name END not defined
thanks again!

oh don't worry about my previous post. I just forgot to import tkinter in that separate file.

thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.