A_Dubbs 0 Light Poster

I recently wrote a thread about help with classes. I got a lot of good responses but I am still struggling with more complicated stuff. I have an old project that I wrote a few weeks into learning python. I am now suppose to turn this project into a class. This project is very linear and is written under one function "main". I did not know much about structuring programs when I wrote it.

My main problem is the structuring of the class and project. It seems I have to put def main(): under the constructor so it can be assigned to an object. How do I go about making modules for it? Is there any way to leave main() as it is and then add funtions to work as modules to call data from main()? I will add the project below to show you what I have done to turn it into a class and maybe someone could tell me what options I have regarding modules and such.

class R:

    def __init__(self):
        
    
        def main():
            #Create graphics window that user will use
            win = GraphWin("Enviornmental Model Project", 640, 480)
            #Set the coordinates of the graphics window to 10 by 10
            win.setCoords(0,0,10,10)
            #Set background colors using Red Green Blue
            win.setBackground(color_rgb(112, 140, 76))

            #Create a button in the lower middle of the screen
            button = Rectangle(Point(4,1), Point(6,3))
            button.setWidth(2)
            button.setFill(color_rgb( 151, 84, 79))
            button.draw(win)
            #Create text for the button. Text may need to change later
            buttontext = Text(Point(5,2), "Enter")
            buttontext.draw(win)
        

            #Get a username from the user
            message = Text(Point(5,7.5),"Hello!  What is your name?")
            message.setFace('times roman')
            message.setSize(10)
            message.setStyle('bold italic')
            message.draw(win)
            name = Entry(Point(5,6.5),10)
            name.draw(win)
            win.getMouse()
            username = name.getText()
        
        
            #Clear username stuff
            name.undraw()
            message.undraw()
        
            #Tell user program purpose. 
            message = Text(Point(5,9),"Hello %s, this program is useful in showing you \
    how much energy your community\n uses and how that cost can differ depending \
    on what energy sources you use." % (username))
            message.setFace('times roman')
            message.setSize(10)
            message.setStyle('bold')
            message.draw(win)

            #Tell the user what they need to do.
            message2 = Text(Point(5,7.5),"To see the energy usage of your community, \
    enter the population of your town, city, or region:")
            message2.setFace('times roman')
            message2.setSize(10)
            message2.setStyle('bold italic')
            message2.draw(win)

            #Create an input in the graphics for the user.
            enter = Entry(Point(5,6.5),7)
            enter.draw(win)

            #Write a message under the input telling user to input before clicking
            #anywhere
            message3 = Text(Point(5, 6), "Enter number with no commas")
            message3.setStyle('italic')
            message3.setSize(10)
            message3.draw(win)

            #Take the users population
            win.getMouse()
            userinput = enter.getText()
            userinput = eval(userinput)
        
            #Wipe window of all but button
            message.undraw()
            message2.undraw()
            message3.undraw()

            #Do all calculations dealing with energy
            #Start by taking user input and dividing by the average amount of people
            #in a household
            households = userinput / 2.59
            # 2.59 is from:
            #http://www.census.gov/population/socdemo/hh-fam/cps2003/tabAVG1.pdf
            households = float(households)
            #Take the number of households and multiply by average number of
            #14,000 kilowatt-hours (average)
            energyinyear = households * 14000
            # 14,000 is from:
            #http://resources.yesican-science.ca/energy_flow/compare2_activity.html
            #To get percent, divide energyinyear by the average (taken by using the
            #same numbers up above for the entire USA pop).
            percent = energyinyear / 1044838435300

            #All the following numbers are taken from:
            #http://peswiki.com/index.php/Directory:Cents_Per_Kilowatt-Hour
        
            #Find the price of coal in cents(range)
            coalprice1 = energyinyear * 3.9
            coalprice2 = energyinyear * 4.4
            #And convert to dollars
            coalprice1 = coalprice1 /100
            coalprice2 = coalprice2 /100
            #Find the price of nuclear in cents(range)
            nuclearprice1 = energyinyear * 11.1
            nuclearprice2 = energyinyear * 14.5
            #And convert to dollars
            nuclearprice1 = nuclearprice2 / 100
            nuclearprice2 = nuclearprice2 / 100
            #Find the price of gas in cents(range)
            gasprice1 = energyinyear * 3.9
            gasprice2 = energyinyear * 4.4
            #And convert to dollars
            gasprice1 = gasprice1 / 100
            gasprice2 = gasprice2 / 100
            #Find the price of wind in cents(range)
            windprice1 = energyinyear * 4.0
            windprice2 = energyinyear * 6.0
            #And convert to dollars
            windprice1 = windprice1 /100
            windprice2 = windprice2 /100
            #Find the price of hydro in cents(range)
            hydroprice1 = energyinyear * 5.1
            hydroprice2 = energyinyear * 11.3
            #And convert to dollars
            hydroprice1 = hydroprice1 /100
            hydroprice2 = hydroprice2 /100
        
            #Create new message
            message = Text(Point(5,9),"The average American household has 2.59 people.\
    Given your population, there are about\n %0.0f households in your area. At \
    14,000 kilowatt-hours per household per year, your area uses\n about %0.0d \
    kilowatt-hours of energy in a year.\n\n This is %0.9f percent of the national \
    average, 1,044,838,435,300 killowatt-hours!" \
    % (households, energyinyear, percent))
            message.setFace('times roman')
            message.setSize(10)
            message.setStyle('bold')
            message.draw(win)

            #Delete user input box and button text
            enter.undraw()
            buttontext.undraw()
        
            #Add another message, giving information.
            message1 = Text(Point(5,7), "\n____________________________________________\
    _____________________________________________________\n \nOne type of energy \
    is coal. 50% percent of the energy produced by the United States is coal.\n \
    Coal is bad for the environment because it releases Co2 into the atmosphere \
    after being\n turned into energy. One ton of coal can produce 2,500 killo-watt \
    hours of energy.\n\nCoal is plentiful but terrible for the enviroment as it \
    produces CO2 into the atmosphere. ")
            message1.setFace('times roman')
            message1.setSize(10)
            message1.setStyle('bold')
            message1.draw(win)


            #Add another message, giving price of coal.
            message5 = Text(Point(5,5), "It would cost between $%0.2f and $%0.2f to \
    pay for the bill just using coal alone." % (coalprice1,coalprice2))
            message5.setFace('times roman')
            message5.setSize(10)
            message5.setStyle('bold')
            message5.draw(win)

            #Tell user to click to continue
            buttontext = Text(Point(5,2), "Continue")
            buttontext.draw(win)
            win.getMouse()
        
            #Clear page again
            message.undraw()
            message1.undraw()
            message5.undraw()

            #Write another message dealing with prices and gas / nuclear energy
            message1 = Text(Point(5,9), "The other two types of energy commonly used \
    are gas and nuclear. Nuclear is more\n expensive but cleaner while gas is less \
    expensive but is let out as a greenhouse gas.\n\n Using just nuclear energy \
    the bill would be between $%0.2f and $%0.2f while the bill\n would be between \
    $%0.2f and $%0.2f using just natural gas." \
    % (nuclearprice1, nuclearprice2, gasprice1, gasprice2))
            message1.setSize(10)
            message1.setStyle('bold')
            message1.draw(win)

            #Write one more message, dealing with alternative energies
            message4 = Text(Point(5,6), "Believe it or not renewable energy sources \
    are, for the most part, just as cheap as the fuels\n we use today as shown \
    below:\n\nWind: Between %0.2f and %0.2f.\nHydro: Between %0.2f and %0.2f.\n\n\
    The only problem using these technologies is the fact that they are difficult. \
    For example,\n wind turbines are big, bulky, loud and unstable in bad weather. \
    Hyrdo plants can\n only be built in certain places and can damage the \
    aquatic eco-system." % (windprice1, windprice2, hydroprice1, hydroprice2))
            message4.setSize(10)
            message4.setStyle('bold')
            message4.draw(win)
        
            #Clear and continue
            win.getMouse()
            buttontext.undraw()
            message1.undraw()
            message4.undraw()
        
            #Create Exit "button" (text)
            buttontext = Text(Point(5,2), "Exit")
            buttontext.draw(win)

            #Give an exit message

            message4 = Text(Point(5,8), "This program shows why you should go out and \
    support renewable energy. Although it has flaws,\n renewable energy is the way \
    of the future and will not cost you much (or any) more money.\n Solar power is \
    still in development and currently costs more to produce, so much that it \
    isn't\n cost effiecent at the moment. However, you can still use panels for \
    indvidual homes and\n save money.\n\n More information about solar energy can \
    be found at http://www.solarenergy.org .")
            message4.setSize(10)
            message4.setStyle('bold')
            message4.draw(win)

            win.getMouse()
            win.close()

        
        if __name__ == "__main__": main()
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.