-1

i'm trying to learn Python and QT4. I like pizza, so I realized a program to calculate the amount of ingredients, based on the number of pizzas I want to make.

The program is the following and do its job.

class Pizza:
    # MEMBRI
    farina=0
    acqua=0
    lievito=0
    sale=0

    def stampaIngredientiPizza(self, f, a, l, s):
        self.stampaFarina=f
        self.stampaAcqua=a
        self.stampaLievito=l
        self.stampaSale=s

        print("Farina 00 (W240):            ", self.stampaFarina, "gr")
        print("Acqua:                       ", self.stampaAcqua, "gr")
        print("Lievito di birra:            ", self.stampaLievito, "gr")
        print("Sale:                        ", self.stampaSale, "gr")


class Romana(Pizza):
    # MEMBRI
    olio=0
    semola=0


class Napoletana(Pizza):
    # MEMBRI
    strutto=0
    manitoba=0


# INIZIALIZZAZIONE OGGETTI
#
# TONDA ROMANA
#
tondaRomana=Romana()
tondaRomana.farina=80
tondaRomana.semola=20
tondaRomana.acqua=57
tondaRomana.lievito=0.171
tondaRomana.sale=2.85
tondaRomana.olio=2.85


#
# TEGLIA ROMANA
#
# TUTTI I VALORI VANNO MOLTIPLICATI X 1,5
#


#
# TONDA NAPOLETANA
#
tondaNapoletana=Napoletana()
tondaNapoletana.farina=72
tondaNapoletana.manitoba=48
tondaNapoletana.acqua=72
tondaNapoletana.lievito=0.216
tondaNapoletana.sale=3.6
tondaNapoletana.strutto=7.2


#
# TEGLIA NAPOLETANA
#
# TUTTI I VALORI VANNO MOLTIPLICATI X 1,5
#


romaNapoli=-1                   # Romana o Napoleta
tondaTeglia=-1                  # Tonda o Teglia

print("\t\t\tPIZZA ROMANA o NAPOLETANA\n\n")

while(romaNapoli!=0):

    print("1) Pizza Romana")
    print("2) Pizza Napoletana")
    print("0) Uscita")

    romaNapoli=int(input("\nSelezione (0, 1 ,2)? "))


    if(romaNapoli==1):          ##### PIZZA ROMANA #####
        while(tondaTeglia!=0):
            print()

            print("1) Tonda 30cm")
            print("2) Teglia 40x35cm")
            print("0) Uscita")

            tondaTeglia=int(input("\nSelezione (0, 1 ,2)? ")) ##### TONDA O TEGLIA #####


            if(tondaTeglia==1):     # TONDA     
                quantePizze=eval(input("\nQuante pizze tonde da 30cm vuoi preparare? "))
                print()

                totaleImpasto=(tondaRomana.farina*quantePizze+tondaRomana.semola*quantePizze+
                               tondaRomana.acqua*quantePizze+tondaRomana.olio*quantePizze+tondaRomana.sale*quantePizze)

                if(quantePizze==1):
                    print("Per preparare", quantePizze, "pizza Romana in teglia tonda, ti occorrono:")
                    print()
                else:
                    print("Per preparare", quantePizze, "pizze Romane in teglia tonda, ti occorrono:")
                    print()            

                print("Farina di semola rimacinata: ", tondaRomana.semola*quantePizze, "gr")

                tondaRomana.stampaIngredientiPizza(tondaRomana.farina*quantePizze, tondaRomana.acqua*quantePizze, tondaRomana.lievito*quantePizze, tondaRomana.sale*quantePizze)

                print("Olio:                        ", tondaRomana.olio*quantePizze, "gr")
                print()
                print("Totale impasto:              ", totaleImpasto, "gr\n")
                premiUnTasto=input("Premi INVIO per continuare")
                print()


            if(tondaTeglia==2):     # TEGLIA
                quantePizze=eval(input("\nQuante pizze in teglia 40x35cm vuoi preparare? "))
                print()

                totaleImpasto=(tondaRomana.farina*1.5*quantePizze+tondaRomana.semola*1.5*quantePizze+
                               tondaRomana.acqua*1.5*quantePizze+tondaRomana.olio*1.5*quantePizze+tondaRomana.sale*1.5*quantePizze)

                if(quantePizze==1):
                    print("Per preparare", quantePizze, "pizza Romana in teglia rettangolare, ti occorrono:")
                    print()
                else:
                    print("Per preparare", quantePizze, "pizze Romane in teglia rettangolare, ti occorrono:")
                    print()

                print("Farina di semola rimacinata: ", tondaRomana.semola*1.5*quantePizze, "gr")

                tondaRomana.stampaIngredientiPizza(tondaRomana.farina*1.5*quantePizze, tondaRomana.acqua*1.5*quantePizze, tondaRomana.lievito*1.5*quantePizze,
                                                   tondaRomana.sale*1.5*quantePizze)

                print("Olio:                        ", tondaRomana.olio*1.5*quantePizze, "gr")
                print()
                print("Totale impasto:              ", totaleImpasto, "gr\n")
                premiUnTasto=input("Premi INVIO per continuare")
                print()


    if(romaNapoli==2):          ##### PIZZA NAPOLETANA #####
        print()

        print("1) Tonda 30cm")
        print("2) Teglia 40x35cm")
        print("0) Uscita")

        tondaTeglia=int(input("\nSelezione (0,1 ,2)? ")) ##### TONDA O TEGLIA #####
        if(tondaTeglia==0):
            break

        if(tondaTeglia==1):     # TONDA 
            quantePizze=eval(input("\nQuante pizze tonde da 30cm vuoi preparare? "))
            print()        

            totaleImpasto=(tondaNapoletana.farina*quantePizze+tondaNapoletana.manitoba*quantePizze+
                           tondaNapoletana.acqua*quantePizze+tondaNapoletana.strutto*quantePizze+
                           tondaNapoletana.sale*quantePizze)

            if(quantePizze==1):
                print("Per preparare", quantePizze, "pizza Napoletana in teglia tonda, ti occorrono:")
                print()
            else:        
                print("Per preparare ", quantePizze, "pizze Napoletane in teglia tonda, ti occorrono:")
                print()

            print("Farina manitoba :            ", tondaNapoletana.manitoba*quantePizze, "gr")

            tondaNapoletana.stampaIngredientiPizza(tondaNapoletana.farina*quantePizze, tondaNapoletana.acqua*quantePizze, tondaNapoletana.lievito*quantePizze,
                                                   tondaNapoletana.sale*quantePizze)

            print("Strutto:                     ", tondaNapoletana.strutto*quantePizze, "gr")
            print()
            print("Totale impasto:              ", totaleImpasto, "gr\n")
            premiUnTasto=input("Premi INVIO per continuare")
            print()

        if(tondaTeglia==2):     # TEGLIA
            quantePizze=eval(input("\nQuante pizze in teglia 40x35cm vuoi preparare? "))
            print()        

            totaleImpasto=(tondaNapoletana.farina*1.5*quantePizze+tondaNapoletana.manitoba*1.5*quantePizze+
                           tondaNapoletana.acqua*1.5*quantePizze+tondaNapoletana.strutto*1.5*quantePizze+
                           tondaNapoletana.sale*1.5*quantePizze)

            if(quantePizze==1):
                print("Per preparare", quantePizze, "pizza Napoletana in teglia rettangolare, ti occorrono:")
                print()
            else:        
                print("Per preparare ", quantePizze, "pizze Napoletane in teglia rettangolare, ti occorrono:")
                print()

            print("Farina manitoba :            ", tondaNapoletana.manitoba*1.5*quantePizze, "gr")

            tondaNapoletana.stampaIngredientiPizza(tondaNapoletana.farina*1.5*quantePizze, tondaNapoletana.acqua*1.5*quantePizze, tondaNapoletana.lievito*1.5*quantePizze,
                                                   tondaNapoletana.sale*1.5*quantePizze)

            print("Strutto:                     ", tondaNapoletana.strutto*1.5*quantePizze, "gr")
            print()
            print("Totale impasto:              ", totaleImpasto, "gr\n")
            premiUnTasto=input("Premi INVIO per continuare")
            print()

First there is a selection based on what kind of pizza to make: Romana or Napoletana then the shape: Tonda(round) or Rettangolare(rectangular) then the q.ty and there is a printout of the ingredients.

there are 4 basic ingredients for pizza: flour, yeast, water, salt. To make Romana must add oil and semolina to the 4 basics. To make Napoletana must add lard and manitoba to the 4 basics

so there is a Base Class Pizza and two Derived Classes Romana and Napoletana

Now I want to give a graphical interface to the program. I wrote this code but have problems because of my lack of knowledge

import sys
from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):

    def __init__(self):

        QtGui.QMainWindow.__init__(self)

        self.resize(750, 250)
        self.setWindowTitle("CALCOLATORE DOSI IMPASTO PER PIZZA ROMANA O NAPOLETANA")
        #self.setWindowIcon(QtGui.QIcon('pizza.jpg'))  

        self.schermataRomaNapoli()    

    # SCHERMATA SELEZIONE TIPO IMPASTO DA REALIZZARE: ROMANA O NAPOLETANA
    def schermataRomaNapoli(self):

        cWidget = QtGui.QWidget(self)
        grid = QtGui.QGridLayout(cWidget) 

        vBox = QtGui.QVBoxLayout()
        etichetta1 = QtGui.QLabel("Seleziona il tipo di pizza che vuoi preparare:", cWidget)
        etichetta1.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        radio1 = QtGui.QRadioButton("Pizza Romana", cWidget)
        radio1.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        radio1.clicked.connect(self.romana)
        radio2 = QtGui.QRadioButton("Pizza Napoletana", cWidget)
        radio2.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        radio2.clicked.connect(self.napoletana)
        radio3 = QtGui.QRadioButton("Uscita", cWidget)
        radio3.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        radio3.clicked.connect(self.close)
        vBox.addWidget(etichetta1)
        vBox.addWidget(radio1)
        vBox.addWidget(radio2)
        vBox.addWidget(radio3)         

        grid.addLayout(vBox, 1, 0)

        cWidget.setLayout(grid)
        self.setCentralWidget(cWidget)

    # PIZZA ROMANA
    def romana(self):

        global romaNapoli
        romaNapoli=" Romana "
        olio=0
        semola=0

        self.schermataTondaTeglia()

    # PIZZA NAPOLETANA
    def napoletana(self):

        global romaNapoli
        romaNapoli=" Napoletana "
        strutto=0
        manitoba=0

        self.schermataTondaTeglia()

    # SCHERMATA SELEZIONE FORMATO TEGLIA: TONDA 30cm O RETTANGOLARE 40x35cm
    def schermataTondaTeglia(self):

        cWidget = QtGui.QWidget(self)
        grid = QtGui.QGridLayout(cWidget) 

        vBox = QtGui.QVBoxLayout()
        etichetta1 = QtGui.QLabel("Seleziona il tipo di formato che vuoi preparare:", cWidget)
        etichetta1.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        radio1 = QtGui.QRadioButton("Tonda 30cm", cWidget)
        radio1.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        radio1.clicked.connect(self.schermataQuanteTonde)
        radio2 = QtGui.QRadioButton("Teglia 40x35cm", cWidget)
        radio2.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        radio2.clicked.connect(self.schermataQuanteRettangolari)
        radio3 = QtGui.QRadioButton("Indietro", cWidget)
        radio3.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        radio3.clicked.connect(self.schermataRomaNapoli)
        vBox.addWidget(etichetta1)
        vBox.addWidget(radio1)
        vBox.addWidget(radio2)
        vBox.addWidget(radio3)         

        grid.addLayout(vBox, 1, 0)

        cWidget.setLayout(grid)
        self.setCentralWidget(cWidget)

    # SCHERMATA SELEZIONE QUANTE PIZZE TONDE DA REALIZZARE
    def schermataQuanteTonde(self):

        global quantePizze, pizzaPizze, romaNapoli, tondaRettangolare
        quantePizze = QtGui.QInputDialog.getInt(self, "CALCOLATORE DOSI IMPASTO PER PIZZA ROMANA O NAPOLETANA", "Quante pizze tonde da 30cm vuoi preparare?", 1)       

        tondaRettangolare=" tonda"

        if(quantePizze[0]==1):
            pizzaPizze=" pizza"            

        # SE LE PIZZE SONO PIU' DI UNA, LE PAROLE PIZZA, ROMANA E NAPOLETANA DIVENTANO PLURALI: PIZZE, ROMANE, NAPOLETANE
        if(quantePizze[0]!=1):  
            pizzaPizze=" pizze"
            if(romaNapoli==" Romana "):
                romaNapoli=" Romane "
            else:
                romaNapoli=" Napoletane "

        if(romaNapoli==" Romana "):
            Romana()                        # VA SULLA CLASSE ROMANA(PIZZA)

        if(romaNapoli==" Napoletana "):
            Napoletana()                    # VA SULLA CLASSE NAPOLETANA(PIZZA)

    # SCHERMATA SELEZIONE QUANTE PIZZE RETTANGOLARI DA REALIZZARE
    def schermataQuanteRettangolari(self):

        global quantePizze, pizzaPizze, romaNapoli, tondaRettangolare
        quantePizze = QtGui.QInputDialog.getInt(self, "CALCOLATORE DOSI IMPASTO PER PIZZA ROMANA O NAPOLETANA", "Quante pizze rettangolari da 40x35cm vuoi preparare?", 1)

        tondaRettangolare=" rettangolare"

        if(quantePizze[0]==1):
            pizzaPizze=" pizza"

        # SE LE PIZZE SONO PIU' DI UNA, LE PAROLE PIZZA, ROMANA E NAPOLETANA DIVENTANO PLURALI: PIZZE, ROMANE, NAPOLETANE
        if(quantePizze[0]!=1):
            pizzaPizze=" pizze"
            if(romaNapoli==" Napoletana "):
                romaNapoli=" Napoletane "
            else:
                romaNapoli=" Romane " 

        if(romaNapoli==" Romana "):
            Romana()                        # VA SULLA CLASSE ROMANA(PIZZA)

        if(romaNapoli==" Napoletana "):
            Napoletana()                    # VA SULLA CLASSE NAPOLETANA(PIZZA)        


class Pizza(QtGui.QMainWindow):

    global farina, acqua, lievito, sale
    # INGREDIENTI PIZZA BASE
    farina=0
    acqua=0
    lievito=0
    sale=0  

    def __init__(self):

        QtGui.QMainWindow.__init__(self)

        self.resize(750, 250)
        self.setWindowTitle("CALCOLATORE DOSI IMPASTO PER PIZZA ROMANA O NAPOLETANA")
        self.setWindowIcon(QtGui.QIcon('pizza.jpg'))

        self.stampaIngredientiPizza(farina, acqua, lievito, sale)    

    # STAMPA INGREDIENTI BASE
    def stampaIngredientiPizza(self, f, a, l, s):
        stampaFarina=f
        stampaAcqua=a
        stampaLievito=l
        stampaSale=s

        # DA TOGLIERE
        quantePizze=['2']     
        pizzaPizze=" pizze"
        romaNapoli=" Romane "
        tondaRettangolare=" tonda"
        # DA TOGLIERE

        cWidget = QtGui.QWidget()

        grid = QtGui.QGridLayout(cWidget) 

        vBox = QtGui.QVBoxLayout()
        etichetta11 = QtGui.QLabel("Per preparare " + str(quantePizze[0])+ pizzaPizze + romaNapoli + "in teglia" + tondaRettangolare + ", ti occorrono:\n", cWidget)
        etichetta11.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        etichetta1 = QtGui.QLabel("Farina 00 (W240): " + str(stampaFarina) + "gr", cWidget)
        etichetta1.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        etichetta2 = QtGui.QLabel("Acqua:                    " + str(stampaAcqua) + "gr", cWidget)
        etichetta2.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        etichetta3 = QtGui.QLabel("Lievito di birra:     " + str(stampaLievito) + "gr", cWidget)
        etichetta3.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        etichetta4 = QtGui.QLabel("Sale:                        " + str(stampaSale) + "gr", cWidget)
        etichetta4.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        vBox.addWidget(etichetta11)
        vBox.addWidget(etichetta1)
        vBox.addWidget(etichetta2)
        vBox.addWidget(etichetta3)
        vBox.addWidget(etichetta4)        

        grid.addLayout(vBox, 1, 0)

        cWidget.setLayout(grid)
        self.setCentralWidget(cWidget)

# IMPASTO TONDA ROMANA
class Romana(Pizza):        

    tondaRettangolare=" tonda"      # DA TOGLIERE
    if(tondaRettangolare==" tonda"):

        def stampaIngredientiPizza(self, f, a, l, s):

            farina=80
            semola=20
            acqua=57
            lievito=0.171
            sale=2.85
            olio=2.85

            super(Romana, self).stampaIngredientiPizza(farina, acqua, lievito, sale)





app=QtGui.QApplication(sys.argv)
main=MainWindow()
main.show()
sys.exit(app.exec_())

The first part of the code (MainWindow) works. If I remove the 3 ''' for Pizza Class the program don't work.

It should print in the window, the 4 basic ingredients (stampaIngredientiPizza method). It should add in the same window, the 2 more ingredients (tondaRomana method) depending on the kind of pizza to make.

Determined that the mainWindow Class work, I would like your help to run the Class Pizza and then put everything together. I edited the below code. Now I have this error: NameError: name 'stampaIngredientiPizza' is not defined

    class Pizza(QtGui.QMainWindow):

    global farina, acqua, lievito, sale
    # INGREDIENTI PIZZA BASE
    farina=0
    acqua=0
    lievito=0
    sale=0  

    def __init__(self):

        QtGui.QMainWindow.__init__(self)

        self.resize(750, 250)
        self.setWindowTitle("CALCOLATORE DOSI IMPASTO PER PIZZA ROMANA O NAPOLETANA")
        self.setWindowIcon(QtGui.QIcon('pizza.jpg'))

        self.stampaIngredientiPizza(farina, acqua, lievito, sale)    

    # STAMPA INGREDIENTI BASE
    def stampaIngredientiPizza(self, f, a, l, s):
        stampaFarina=f
        stampaAcqua=a
        stampaLievito=l
        stampaSale=s

        # DA TOGLIERE
        quantePizze=['2']     
        pizzaPizze=" pizze"
        romaNapoli=" Romane "
        tondaRettangolare=" tonda"
        # DA TOGLIERE

        cWidget = QtGui.QWidget()

        grid = QtGui.QGridLayout(cWidget) 

        vBox = QtGui.QVBoxLayout()
        etichetta11 = QtGui.QLabel("Per preparare " + str(quantePizze[0])+ pizzaPizze + romaNapoli + "in teglia" + tondaRettangolare + ", ti occorrono:\n", cWidget)
        etichetta11.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        etichetta1 = QtGui.QLabel("Farina 00 (W240): " + str(stampaFarina) + "gr", cWidget)
        etichetta1.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        etichetta2 = QtGui.QLabel("Acqua:                    " + str(stampaAcqua) + "gr", cWidget)
        etichetta2.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        etichetta3 = QtGui.QLabel("Lievito di birra:     " + str(stampaLievito) + "gr", cWidget)
        etichetta3.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        etichetta4 = QtGui.QLabel("Sale:                        " + str(stampaSale) + "gr", cWidget)
        etichetta4.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        vBox.addWidget(etichetta11)
        vBox.addWidget(etichetta1)
        vBox.addWidget(etichetta2)
        vBox.addWidget(etichetta3)
        vBox.addWidget(etichetta4)        

        grid.addLayout(vBox, 1, 0)

        cWidget.setLayout(grid)
        self.setCentralWidget(cWidget)

# IMPASTO TONDA ROMANA
class Romana(Pizza):        

    tondaRettangolare=" tonda"      # DA TOGLIERE
    if(tondaRettangolare==" tonda"):

        def stampaIngredientiPizza(self, f, a, l, s):

            farina=80
            semola=20
            acqua=57
            lievito=0.171
            sale=2.85
            olio=2.85

            super(Romana, self).stampaIngredientiPizza(farina, acqua, lievito, sale)

app=QtGui.QApplication(sys.argv)
pizza=Romana()
pizza.show()
sys.exit(app.exec_())
Massimo D. N.
  • 316
  • 1
  • 6
  • 17
  • What "don't work" ? Could you try to shorten the code to have an [MCVE](http://stackoverflow.com/help/mcve) ? – Mel Nov 20 '15 at 14:13
  • tondaRettangolare in class Romana(Pizza) its not defined although its a global variable. stampaIngredientiPizza(farina, acqua, lievito, sale) in class Romana(Pizza) don't produce printout. Commenting Pizza Class and its subclasses, the MainWindows works until the ingredients printout – Massimo D. N. Nov 20 '15 at 16:17
  • Determined that the mainWindow Class work, I would like your help to run the Class Pizza and then put everything together. I edited the below code. Now I have this error: NameError: name 'stampaIngredientiPizza' is not defined – Massimo D. N. Nov 21 '15 at 15:24
  • I make an MCVE. The last code with only two classes. Now I have this error: NameError: name 'stampaIngredientiPizza' is not defined. I'm not able to launch the stampaIngredientiPizza method from the Romana(Pizza) Class – Massimo D. N. Nov 23 '15 at 11:06
  • Last code modified and working using the super class. Now the problem is how to call class Romana(Pizza) from the MainWindow – Massimo D. N. Nov 23 '15 at 14:24

1 Answers1

0

You shouldn't have to use globals. Globals are really bad when making GUI's. You could want multiple items of something, but you are dependent on the value from a global. You are also manipulating class variables when you want to manipulate instance variables. You should instantiate variables within your MainWindow.

Example:

class Pizza(object):
    '''Create a pizza'''

    def __init__(self):
        self.farina=0
        self.acqua=0
        self.lievito=0
        self.sale=0
        self.olio=0
        self.semola=0
        self.strutto=0
        self.manitoba=0

        self.set_default_ingredients()
    # end Constructor

    @classmethod
    def default_ingredients(cls):
        '''Return the default ingredients for a pizza.'''
        return {'farina':0, 'acqua':0, 'lievito':0, 'sale':0}

    def set_default_ingredients(self):
        '''Set the default ingredients.'''
        self.farina=0
        self.acqua=0
        self.lievito=0
        self.sale=0
        self.olio=0
        self.semola=0
        self.strutto=0
        self.manitoba=0
    # end set_default_ingredients

    def bake(self):
        '''Bake the pizza.'''
        pass # Do something to bake the pizza.
    # end class Pizza

class Romana(Pizza):
    '''Create a special Romana Pizza.'''

    @classmethod
    def default_ingredients(cls):
        '''Return the default ingredients for a Romana Pizza.'''
        return {'farina':80, 'semola':20, 'acqua':57, 'lievito':0.171, 'sale':2.85, 'olio':2.85}

    def set_default_ingredients(self):
        '''Set the default ingredients for a Romana Pizza.'''
        self.farina=80
        self.semola=20
        self.acqua=57
        self.lievito=0.171
        self.sale=2.85
        self.olio=2.85

class Napoletana(Pizza):
    '''Create a special Napoletana Pizza.'''

    @classmethod
    def default_ingredients(cls):
        '''Return the default ingredients for a Romana Pizza.'''
        return {'farina':72, 'manitoba':48, 'acqua':72, 'lievito':0.216, 'sale':3.6, 'strutto':7.2}

    def set_default_ingredients(self):
        '''Set the default ingredients for a Romana Pizza.'''
        self.farina=72
        self.manitoba=48
        self.acqua=72
        self.lievito=0.216
        self.sale=3.6
        self.strutto=7.2


class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super().__init__()

        self.main_widg = QtGui.QWidget()
        self.main_layout = QtGui.QFormLayout()
        self.main_widg.setLayout(self.main_layout)
        self.setCentralWidget(self.main_widg)

        self.current_pizza = Pizza()

        self.pizza_type = QtGui.QComboBox()
        self.pizza_type.addItems(['Pizza', 'Romana', 'Napoletana'])
        self.pizza_type.currentIndexChanged.connect(self.select_pizza)

        # Ingredients
        self.farina = QtGui.QLineEdit('0')
        self.farina.textChanged.connect(self.set_farina)
        self.acqua = QtGui.QLineEdit('0')
        self.acqua.textChanged.connect(self.set_acqua)
        self.lievito = QtGui.QLineEdit('0')
        self.lievito.textChanged.connect(self.set_lievito)
        self.sale = QtGui.QLineEdit('0')
        self.sale.textChanged.connect(self.set_sale)
        self.olio = QtGui.QLineEdit('0')
        self.olio.textChanged.connect(self.set_olio)
        self.semola = QtGui.QLineEdit('0')
        self.semola.textChanged.connect(self.set_semola)
        self.strutto = QtGui.QLineEdit('0')
        self.strutto.textChanged.connect(self.set_strutto)
        self.manitoba = QtGui.QLineEdit('0')
        self.manitoba.textChanged.connect(self.set_manitoba)

        # Actions
        self.bake_btn = QtGui.QPushButton("Bake")
        self.bake_btn.clicked.connect(self.bake)

        # Layout
        self.main_layout.addRow('Pizza Type', self.pizza_type)
        self.main_layout.addRow('farina', self.farina)
        self.main_layout.addRow('acqua', self.acqua)
        self.main_layout.addRow('lievito', self.lievito)
        self.main_layout.addRow('olio', self.olio)
        self.main_layout.addRow('semola', self.semola)
        self.main_layout.addRow('strutto', self.strutto)
        self.main_layout.addRow('manitoba', self.manitoba)
        self.main_layout.addRow(self.bake_btn)
    # end __init__

    def select_pizza(self, idx):
        if self.pizza_type.currentText() == 'Pizza':
            self.current_pizza = Pizza()
        elif self.pizza_type.currentText() == 'Romana':
            self.current_pizza = Romana()
        else:
            self.current_pizza = Napoletana()
        self.set_values()
    # end select_pizza

    def set_values(self):
        '''Set the values from the current pizza.'''
        self.farina.setText(str(self.current_pizza.farina))
        self.acqua.setText(str(self.current_pizza.acqua))
        self.lievito.setText(str(self.current_pizza.lievito))
        self.sale.setText(str(self.current_pizza.sale))
        self.olio.setText(str(self.current_pizza.olio))
        self.semola.setText(str(self.current_pizza.semola))
        self.strutto.setText(str(self.current_pizza.strutto))
        self.manitoba.setText(str(self.current_pizza.manitoba))

    def set_farina(self, val):
        self.current_pizza.farina = val
    def set_acqua(self, val):
        self.current_pizza.acqua = val
    def set_lievito(self, val):
        self.current_pizza.lievito = val
    def set_sale(self, val):
        self.current_pizza.sale = val
    def set_olio(self, val):
        self.current_pizza.olio = val
    def set_semola(self, val):
        self.current_pizza.semola = val
    def set_strutto(self, val):
        self.current_pizza.strutto = val
    def set_manitoba(self, val):
        self.current_pizza.manitoba = val

    def bake(self):
        self.current_pizza.bake()
# end class MainWindow


if __name__ == "__main__":
    import sys
    QtGui.QApplication([])

    window = MainWindow()
    window.show()

    sys.exit(QtGui.qApp.exec_())

The main point of this user interface is to interact with the current_pizza values. By changing the pizza type you change the default values that are displayed. Changing the values in the GUI changes the object you are working with. The instance is contained in the main window, so you could call MainWindow() twice and have two working main windows that are independent of each other.

In this example I also included all of the different toppings and ingredients in the base class Pizza. This makes it so any one can make a Pizza with any ingredient that you have available. If you select a specialty pizza like Romana then the user interface will populate the values with that pizza type, and the user can adjust it the way they want.

justengel
  • 6,132
  • 4
  • 26
  • 42
  • Thank you very much. Now I have a lot to study, but first of all, if I run the program, nothing append (I also included: import sys and from PyQt4 import QtGui, QtCore lines at TOP) – Massimo D. N. Nov 24 '15 at 07:58
  • I just gave you an example of how to write the code. The program isn't supposed to do anything. – justengel Nov 24 '15 at 13:01
  • I mean [link](www.mediafire.com/view/akdb9gzodhh48st/pizza.png#) – Massimo D. N. Nov 25 '15 at 08:45
  • 1
    Are you running it as main? The statement ''if __name__ == "__main__":'' makes it so that the code only runs if that is the main file. This way on another project you can use ''import pizza'' and call ''pizza.Romana()'' from another file or project. http://www.effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm – justengel Nov 25 '15 at 13:45
  • Than you so much. If I use app=QtGui.QApplication(sys.argv) main=MainWindow() main.show() sys.exit(app.exec_()) at he end of the code, instead of if __name__ == "__main__": etc the program run. I created a.py import pizza pizza.MainWindow() nothing append. Were is my error? – Massimo D. N. Nov 26 '15 at 08:59
  • 1
    MainWindow is the class. You have to have an application to run the GUI. Even if you ''import pizza'' and call ''w = pizza.MainWindow()'' ''w.show()''. You still have to call ''app = QtGui.QApplication([])'' and ''sys.exit(app.exec_())''. – justengel Nov 28 '15 at 20:18